Admob ads appear on all scenes.

Hey guys,

I have gotten my Ads working in my game using the following script ( which is attached to the camera in my main menu)

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;

public class Request2 : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        RequestBanner();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void RequestBanner()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "///";
#elif UNITY_IPHONE
        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif

        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }
}

It works fine. However, I want the ads to disappear when I get into my actual gameplay. It can appear on any other level just NOT the gameplay. I’ve tried seemingly every solution , and the ads still show up every scene. So , how can I make my ads show up on all levels except my gameplay?

I don’t see anything in your code that detects what scene you’re in.

You’ll need something like:

if (Application.loadedLevel == YourGameScene)
{
    //Hide your ad, something like bannerView.Hide()
}

Still doesn’t work. I’ve tried this before , and I think it doesn’t work because the Application class is now obsolete. So , what is the equivalent in the SceneManager class?

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;

public class Request : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        RequestBanner();
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void RequestBanner()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "111;
#elif UNITY_IPHONE
        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif

        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner,AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
        if (Application.loadedLevel == 1)
        {
            bannerView.Hide();
        }
    }
}

Application.LoadLevel still works, it’s just deprecated.

This has the equivalent: Unity - Scripting API: SceneManagement.SceneManager.GetActiveScene

Hmm. I even tried bannerView.Destroy() , and it still didn’t work. If Application still works then what am I doing wrong?

Are you sure bannerView.Destroy() is getting called?

Try to add a Debug.Log() to make sure it is trying to close the ad. I’ve used Admob before and it was the most frustrating experience of my life :frowning:

Yeah , this is my first game , and using Admob is frustrating as hell.

Here’s the code. It seems bannerView.destroy isn’t getting called. However , ads don’t show up in the editor anyways.

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;

public class Request : MonoBehaviour
{
    private BannerView bannerView;
    // Use this for initialization
    void Start()
    {
 
        RequestBanner();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void RequestBanner()
    {
#if UNITY_EDITOR
        string adUnitId = "44";
#elif UNITY_ANDROID
        string adUnitId = 55";
#elif UNITY_IPHONE
        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif
        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
        if (Application.loadedLevel == 1)
        {
            bannerView.Destroy();
            Debug.Log("Banner Destroyed");

        }
    }
}

Perhaps I’m using loadedlevel wrong?

Did you try to build the project for Android or iOS and try to run it? I know many of Google’s plugins don’t work in the Unity editor. Perhaps try putting the Debug.Log before banerview.Destroy() to see if it’s getting called at all. If it isn’t then your loaded level isn’t 1. Or you can try to add this to see if the if statement is true:

Debug.Log("Current level is" + Application.loadedLevel);

That way you can see if the loaded level is actually 1.

Alright , the banner destroyed method works. It destroys on level 0 (which is not what I want) However, it keeps telling me the loaded level is 0. Then when I click on the start button ( which leads to scene 1) it still says the current level is 0. Hmm.

EDIT: I built to Android , and the ad takes like 15 seconds to show up ( is there anyway to fix that?)
, and when I go to my actual game , the ad still appears , but is glitchy. I’m not sure if that means Unity is TRYING to delete the ad, but just can’t.

I think I’ve found it out. I attached the following script to my second scene ( the one I want the ad to disappear on) , but I get this error :
NullReferenceException: Object reference not set to an instance of an object
TestLevel.Start () (at Assets/Scripts/TestLevel.cs:10)

Here’s the script :

using UnityEngine;
using System.Collections;

public class TestLevel : MonoBehaviour {
   public Request req; // Reference to my script that generates the banner ad

    // Use this for initialization
    void Start () {
        if (Application.loadedLevel == 1)
        {
            req.bannerView.Destroy(); // This is the script that causes the error
            Debug.Log(" Banner Destroyed");
        }
    }
  
    // Update is called once per frame
    void Update () {
  
    }
}

Other script ( The one you saw before)

using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
public class Request : MonoBehaviour
{
    public  BannerView bannerView;
  
    // Use this for initialization
    public void Start()
    {

#if UNITY_EDITOR
        string adUnitId = "11";
#elif UNITY_ANDROID
        string adUnitId = "11";
#elif UNITY_IPHONE
        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif
        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd(request);
    }

    // Update is called once per frame
    void Update()
    {
    
    }

}

I think this would work if the error didn’t happen. How do I fix it?

Are you sure you copied the code correctly? It’s kind of impossible to have a NullReferenceExpection on a bracket (line 10 of the TestLevel).

using UnityEngine;
using System.Collections;

public class TestLevel : MonoBehaviour {
   public Request req; // Reference to my script that generates the banner ad

    // Use this for initialization
    void Start () {
        if (Application.loadedLevel == 1)
        {//error here
            req.bannerView.Destroy();
            Debug.Log(" Banner Destroyed");
        }
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

I clicked on the error and now it’s telling me it occurs on the bracket below the if function.

Does req.bannerView.Destroy(); work if you put it in the Request script? Apparently that’s the issue.

Also make sure you dragged a gameObject with the Request script onto the Request field in the TestLevel script.

I tried , and I get this error.
NullReferenceException: Object reference not set to an instance of an object
Request.Start () (at Assets/Scripts/Request.cs:25)

using System.Collections;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.SceneManagement;
public class Request : MonoBehaviour
{
    public  BannerView bannerView;
   public Request req;
    public void Start()
    {

#if UNITY_EDITOR
        string adUnitId = "ca-app-pub-7445639135429800/2022704376";
#elif UNITY_ANDROID
        string adUnitId = "ca-app-pub-7445639135429800/2022704376";
#elif UNITY_IPHONE
        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif
        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
     
        bannerView.LoadAd(request);
        req.bannerView.Destroy();

    }

    // Update is called once per frame
    void Update()
    {
     
    }

}

And this one

using UnityEngine;
using System.Collections;

public class TestLevel : MonoBehaviour {
    public Request req;
    // Use this for initialization
    void Start () {
        if (Application.loadedLevel == 1)
        {//error here
           req .bannerView.Destroy();
            Debug.Log(" Banner Destroyed");
        }
    }
   
    // Update is called once per frame
    void Update () {
   
    }
}

I dragged the GameObject into the slots for both scenes , and I still get the error.

I noticed in line 10 of TestLevel you have an extra space before the dot: req .bannerView.Destroy();

In any case, at least you know the line that isn’t working. Are you doing what’s shown here: http://answers.unity3d.com/questions/908047/destroy-bannerinterstitial-ads-of-admob.html

Are you saying that in TestLevel , I should call the OnDestroy Method?
Here’s the updated script.

using UnityEngine;
using System.Collections;

public class TestLevel : MonoBehaviour
{
    public Request req;
    // Use this for initialization
    void Start()
    {
        OnDestroy();
    }

    // Update is called once per frame
    void Update()
    {

    }

    public void OnDestroy()
    {
        if (Application.loadedLevel == 1)
        {//error here
            req.bannerView.Destroy();
            Debug.Log(" Banner Destroyed");
        }

    }
}

EDIT: Still gives me the error. (I hate admob)

Yeah it looks like bannerView isn’t set up right. I’d say look into this: c# - Unable to hide / destroy AdMob Ads in Unity - Game Development Stack Exchange

If that doesn’t work then it might be a bug that you’d go to the Gihub Admob page for.

Why won’t Unity make banner ads, their video ad integration takes 30 seconds to set up :frowning:

Yeah , I still get the error. I think I’m gonna give up on Admob, Is there any other ad network that provides banner ads and supports Unity (and that isn’t as frustrating to set up as Admob)

I’d still ask on the GitHub Admob page, just in case you could get it to work. I wish I could talk from personal experience, but I haven’t shipped a project yet (however I’m working on something major at the moment ;)). Seems like Millennial Media, Mopub and AirPush are good other options:

http://docs.millennialmedia.com/android-ad-sdk/
http://www.airpush.com/developers/

I used Admob before just to try it out, however I never used the Banner.Destroy() method. Overall I think Google products are pretty low quality when it comes to the developer side of things. Stuff like Google, Gmail, Drive, YouTube is OK but developing for Android and using their plugins is a convoluted mess when compared to other solutions.

If you’re going for iOS, maybe this can work: https://docs.unity3d.com/ScriptReference/iOS.ADBannerView.html

1 Like

I just asked (on the github page). Hopefully I can get an answer. I’ll def try those out if all else fails with Admob. Thanks for all the help man!
I’ll post the solution if I get it!