- Home /
MonoBehaviours can only be added using AddComponent(). Alternatively.
Could I get some help to fix these 2 lines of my code? I know what the problem is but honestly don’t know how to fix it, Ive seen many examples but I don’t know how to apply it to MY code
using System; using UnityEngine; using GoogleMobileAds.Api;
public class AdMob : MonoBehaviour { private BannerView bannerView;
public void Start()
{
MobileAds.Initialize(initStatus => { });
this.RequestBanner();
}
private void RequestBanner()
{
string adUnitId = "ca-app-pub-7396991836824042/1028963955";
this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
this.bannerView.LoadAd(request);
}
}
I never used any advertisement. However it looks like non of the classes in the code you've showed is even a MonoBehaviour except for your enclosing "AdMob" component. So it's very unlikely that a potential error you get comes from this code snippet. Please, read the error carefully. You usually get an exact filename and a line number where the error happend.
Like it was already said, you haven't really added much useful information to your question. Did you actually attach your "AdMob" component to a gameobject in your scene? Do you somehow try to create an instance of your AdMob class manually, somewhere?
Thank you, I actually did fix a bit of the problem by taking another look at the console, now the built goes through, no ad though and get same console message... So i have a little game and i wanted to ad a banner to it using Google Admob. I got the banner to work in unity but i keep getting this warning and of course, the banner does not come up in the apk. The script is attached to a Game Object, I actually followed the Admob guide and a few tutorials, there are actually forums about that same problem, seems to be an easy fix, get rid of "new" somehow using AddComponent() (or so I've read), but I am so new at coding that I don't know how to do it on my code. Maybe this image explains it better
Answer by Bunny83 · Oct 02, 2021 at 06:51 PM
It's an old issue in the Android Admob plugin. This has already been asked in january this year. There also is already an issue submitted to the google github repository about that issue.
As you can see in the issue, there was already an SO question about the same issue.
So the issue is that the library you're using is faulty. It could have never worked in the first place. I don't know if those who have created this library never actually tried it in Unity. As you can read in the other Unity answers question, you may be able to simply remove the "MonoBehaviour" base class from the "ButtonBehaviour" class. However I would also not recommend messing around with the library, especially when you don't understand the innerworkings.
So there's little you can do here. Either you increase the pressure on the github issue page so they may start fixing it some time in the future, or you try to fix it yourself. We won't fix a google library here in UA, sorry.
Yes I saw that issue trend but as i said, i didnt know how to do it on my script. Anyway, I'll figure it out, thank you.
Your answer
Follow this Question
Related Questions
Error trying to createMonoBehaviour using the 'new' keyword in cSharp script 4 Answers
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. 2 Answers
Unity - UI Dropdown uses constructor for DropDownItem 0 Answers
Unity 2D rotation speeds problem 1 Answer
You are trying to create a MonoBehaviour using the 'new' keyword. 2 Answers