0

I have this error in Unity.It shows the piece of code where i added my banner.And piece of code is simply normal like it is shown on official google page;

  private void RequestBanner()
    {
        // Clean up banner before reusing
       if (bannerView != null)
        {
            bannerView.Destroy();
        }
      
            this.bannerView = new BannerView(BannerID, AdSize.Banner,AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request); // <-- Warning thrown HERE
        bannerView.Show();

  // bannerView.Destroy();
 
        

      
    }

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

UnityEngine.MonoBehaviour:.ctor()
ButtonBehaviour:.ctor()
GoogleMobileAds.Unity.BannerClient:CreateButtonBehavior() (at Assets/GoogleMobileAds/Platforms/Unity/BannerClient.cs:64)
GoogleMobileAds.Unity.BannerClient:ShowBannerView() (at Assets/GoogleMobileAds/Platforms/Unity/BannerClient.cs:143)
GoogleMobileAds.Unity.BannerClient:LoadAd(AdRequest) (at Assets/GoogleMobileAds/Platforms/Unity/BannerClient.cs:123)
GoogleMobileAds.Api.BannerView:LoadAd(AdRequest) (at Assets/GoogleMobileAds/Api/BannerView.cs:61)
Admanager:RequestBanner() (at Assets/Scripts/Admanager.cs:115)
Admanager:Start() (at Assets/Scripts/Admanager.cs:63)
Kuba
  • 2,986
  • 2
  • 26
  • 32
bata kos
  • 91
  • 5
  • Is bannerview your code or someone elses. The error is correct. You dont make monobehaviors that way – BugFinder Jan 27 '21 at 22:08
  • [**Please refer to this**](https://imgur.com/a/Qw9ZMHL)! The error already tells you exactly what is wrong ;) See e.g. [here](https://stackoverflow.com/a/62016464/7111561) or [here](https://stackoverflow.com/questions/40640553/why-in-unity-im-getting-the-warning-you-are-trying-to-create-a-monobehaviour-u) or [here](https://stackoverflow.com/a/37399263/7111561) or [here](https://stackoverflow.com/questions/30849855/instantiate-a-class-that-derives-from-monobehaviour) .... – derHugo Jan 28 '21 at 07:39
  • yes i see it clearely ,but in case of bannerView i must Use new,otherwise I cann not call banner more then once in Activity or scene. – bata kos Jan 28 '21 at 13:55
  • @derHugo The error tells what is wrong, but doesn't tell how to fix it HERE. If you read carefully the problem is not in the code shown in OP - no Component is being created here by OP explicitely. The "new" is being called by the GoogleMobileAds library internals and seems that it cannot be avoided. – Kuba Dec 28 '21 at 09:00

1 Answers1

0

You shooukd not create monobehaviour or derived classes with new. Take into accounr that even if the language used in unity is c# there some special thinngs that need to be taken into account. For exmple monobehaviour class doesn't support the null-conditional operator (?.) and the null-coalescing operator (??). Check the docs

Another important thing of monobehaviours is that the "live" inside gameobjects. This is how the unity engine operates and the class is designed to be used as gameObject component. This means that you cannot make a Monobehaviour myMB = new Monobehaviour() wherever you wish in the code and make your logic. Instead yo should do:

GameObject myGO = new GameObject;
Monobehaviour myMB = myGO.AddComponent<Monobehaviour>();

This has the consequence that if you need to use a monobehaviour for anything, you need to have the corresponding gameObject in the scene for this, which sometimes you wont specifically need, apart from the need of handling the monobehaviour itself. This sometimes might seem not as neat or clean as it could have been for your app, but many times there are workarounds to avoid the gameObject creation if you make the effort, but most of the times you start the avoiding the corresponding gameobject creation journey, you end up finally creating it because to avoid it does not make sense.

So to sum up, take into accout that to use any monobehaviour yo need the corresponding gameobject for it to live in. You can try check the docs for better undestanding.

rustyBucketBay
  • 4,320
  • 3
  • 17
  • 47
  • `doesn't support the null-conditional operator (?.) and the null-coalescing operator (??).` .. that's a bit bad formulated in the docs ^^ .. any c# class type does "support" them .. they might just return misleading values ;) Mostly because of Unity's [Custom `==` operator](https://blogs.unity3d.com/es/2014/05/16/custom-operator-should-we-keep-it/) ;) – derHugo Jan 28 '21 at 07:31
  • Not working properly falls into "not supported", doesn't it? Or not supported should be understood as a compilation error in general? Maybe more than bad formulated, not specific enough :) Any how very good clarification, thanks – rustyBucketBay Jan 28 '21 at 07:37
  • Yeah the thing is they can't prevent you as a developer from **using** `??` or `new` on the instance .. but really nobody understands why they only show a warning for `new` instead of immediately throw a hard exception and [show a pop-up](https://imgur.com/a/Qw9ZMHL) to the developer ^^ At least they could tell people what to do instead (1. Use `AddComponent()` or 2. use `new GameObject("name", typeof(TheComponent)` or 3. Use `Instantiate` with a prefab that already has the component) .. would be that simple and people wouldn't need to repeatedly ask this here :D – derHugo Jan 28 '21 at 07:51
  • I guess that for efficiency's sake its useful to have the chance to create a monobehaviour instance, maybe to access some of the instance methods of the monobehaviour class (or any of the inheritance chain, which is quite big). One example I can think if is to start a coroutine in a c# class that does not need to be monobehaviour. I am not actually sure if you are even able to do that, but there should be cases where its worth to create only the monobehaviour instead of the gameobject along with it. – rustyBucketBay Jan 28 '21 at 08:20
  • there is no use case at all ;) The entire API only works if this is an active `MonoBehaviour` on a `GameObject` in the scene ^^ e.g. `Coroutins` will only run on an active and enabled `MonoBehaviour` ^^ It just is stupid that it doesn't throw an error – derHugo Jan 28 '21 at 08:24
  • oh, I see...if there is no such case, it is actually stupid. Then there must be a lower level reason for this I guess... – rustyBucketBay Jan 28 '21 at 08:39