Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Admob ads appear on all scenes.

Discussion in 'Editor & General Support' started by tomowale, Nov 26, 2016.

  1. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using GoogleMobileAds.Api;
    4.  
    5. public class Request2 : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     void Start()
    10.     {
    11.         RequestBanner();
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.  
    18.     }
    19.  
    20.     private void RequestBanner()
    21.     {
    22. #if UNITY_EDITOR
    23.         string adUnitId = "unused";
    24. #elif UNITY_ANDROID
    25.         string adUnitId = "///";
    26. #elif UNITY_IPHONE
    27.         string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    28. #else
    29.         string adUnitId = "unexpected_platform";
    30. #endif
    31.  
    32.         BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
    33.         AdRequest request = new AdRequest.Builder().Build();
    34.         bannerView.LoadAd(request);
    35.     }
    36. }
    37.  
    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?
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    I don't see anything in your code that detects what scene you're in.

    You'll need something like:
    Code (CSharp):
    1. if (Application.loadedLevel == YourGameScene)
    2. {
    3.     //Hide your ad, something like bannerView.Hide()
    4. }
     
  3. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using GoogleMobileAds.Api;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Request : MonoBehaviour
    7. {
    8.  
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.         RequestBanner();
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.  
    19.     }
    20.  
    21.     private void RequestBanner()
    22.     {
    23. #if UNITY_EDITOR
    24.         string adUnitId = "unused";
    25. #elif UNITY_ANDROID
    26.         string adUnitId = "111;
    27. #elif UNITY_IPHONE
    28.        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    29. #else
    30.        string adUnitId = "unexpected_platform";
    31. #endif
    32.  
    33.        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner,AdPosition.Bottom);
    34.        AdRequest request = new AdRequest.Builder().Build();
    35.        bannerView.LoadAd(request);
    36.        if (Application.loadedLevel == 1)
    37.        {
    38.            bannerView.Hide();
    39.        }
    40.    }
    41. }
    42.  
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
  5. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Hmm. I even tried bannerView.Destroy() , and it still didn't work. If Application still works then what am I doing wrong?
     
  6. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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 :(
     
  7. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using GoogleMobileAds.Api;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Request : MonoBehaviour
    7. {
    8.     private BannerView bannerView;
    9.     // Use this for initialization
    10.     void Start()
    11.     {
    12.  
    13.         RequestBanner();
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.  
    20.     }
    21.  
    22.     public void RequestBanner()
    23.     {
    24. #if UNITY_EDITOR
    25.         string adUnitId = "44";
    26. #elif UNITY_ANDROID
    27.         string adUnitId = 55";
    28. #elif UNITY_IPHONE
    29.        string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    30. #else
    31.        string adUnitId = "unexpected_platform";
    32. #endif
    33.        BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
    34.        AdRequest request = new AdRequest.Builder().Build();
    35.        bannerView.LoadAd(request);
    36.        if (Application.loadedLevel == 1)
    37.        {
    38.            bannerView.Destroy();
    39.            Debug.Log("Banner Destroyed");
    40.  
    41.        }
    42.    }
    43. }
    44.  
    45.  
    46.  
    Perhaps I'm using loadedlevel wrong?
     
  8. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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:

    Code (CSharp):
    1. Debug.Log("Current level is" + Application.loadedLevel);
    That way you can see if the loaded level is actually 1.
     
  9. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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.
     
    Last edited: Nov 27, 2016
  10. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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 :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestLevel : MonoBehaviour {
    5.    public Request req; // Reference to my script that generates the banner ad
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         if (Application.loadedLevel == 1)
    10.         {
    11.             req.bannerView.Destroy(); // This is the script that causes the error
    12.             Debug.Log(" Banner Destroyed");
    13.         }
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.  
    19.     }
    20. }
    21.  
    Other script ( The one you saw before)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using GoogleMobileAds.Api;
    4. using UnityEngine.SceneManagement;
    5. public class Request : MonoBehaviour
    6. {
    7.     public  BannerView bannerView;
    8.  
    9.     // Use this for initialization
    10.     public void Start()
    11.     {
    12.  
    13. #if UNITY_EDITOR
    14.         string adUnitId = "11";
    15. #elif UNITY_ANDROID
    16.         string adUnitId = "11";
    17. #elif UNITY_IPHONE
    18.         string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    19. #else
    20.         string adUnitId = "unexpected_platform";
    21. #endif
    22.         BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
    23.         AdRequest request = new AdRequest.Builder().Build();
    24.         bannerView.LoadAd(request);
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.    
    31.     }
    32.  
    33. }
    34.  
    35.  
    36.  
    I think this would work if the error didn't happen. How do I fix it?
     
  11. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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).
     
  12. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestLevel : MonoBehaviour {
    5.    public Request req; // Reference to my script that generates the banner ad
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.         if (Application.loadedLevel == 1)
    10.         {//error here
    11.             req.bannerView.Destroy();
    12.             Debug.Log(" Banner Destroyed");
    13.         }
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.    
    19.     }
    20. }
    21.  
    I clicked on the error and now it's telling me it occurs on the bracket below the if function.
     
  13. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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.
     
  14. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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)

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using GoogleMobileAds.Api;
    4. using UnityEngine.SceneManagement;
    5. public class Request : MonoBehaviour
    6. {
    7.     public  BannerView bannerView;
    8.    public Request req;
    9.     public void Start()
    10.     {
    11.  
    12. #if UNITY_EDITOR
    13.         string adUnitId = "ca-app-pub-7445639135429800/2022704376";
    14. #elif UNITY_ANDROID
    15.         string adUnitId = "ca-app-pub-7445639135429800/2022704376";
    16. #elif UNITY_IPHONE
    17.         string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
    18. #else
    19.         string adUnitId = "unexpected_platform";
    20. #endif
    21.         BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Bottom);
    22.         AdRequest request = new AdRequest.Builder().Build();
    23.      
    24.         bannerView.LoadAd(request);
    25.         req.bannerView.Destroy();
    26.  
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.      
    33.     }
    34.  
    35. }
    36.    
    37.  
    38.  
    And this one

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestLevel : MonoBehaviour {
    5.     public Request req;
    6.     // Use this for initialization
    7.     void Start () {
    8.         if (Application.loadedLevel == 1)
    9.         {//error here
    10.            req .bannerView.Destroy();
    11.             Debug.Log(" Banner Destroyed");
    12.         }
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.     }
    19. }
    20.  
    I dragged the GameObject into the slots for both scenes , and I still get the error.
     
  15. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
  16. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    Are you saying that in TestLevel , I should call the OnDestroy Method?
    Here's the updated script.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class TestLevel : MonoBehaviour
    5. {
    6.     public Request req;
    7.     // Use this for initialization
    8.     void Start()
    9.     {
    10.         OnDestroy();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.  
    17.     }
    18.  
    19.     public void OnDestroy()
    20.     {
    21.         if (Application.loadedLevel == 1)
    22.         {//error here
    23.             req.bannerView.Destroy();
    24.             Debug.Log(" Banner Destroyed");
    25.         }
    26.  
    27.     }
    28. }
    29.  
    EDIT: Still gives me the error. (I hate admob)
     
    Last edited: Nov 28, 2016
  17. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
  18. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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)
     
  19. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    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
     
    tomowale likes this.
  20. tomowale

    tomowale

    Joined:
    Feb 14, 2015
    Posts:
    308
    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!
     
  21. viku_99

    viku_99

    Joined:
    Dec 14, 2016
    Posts:
    10
    you can return object of bannerview and destroy it....
    For request banner simple call this method it will return bannerview object call it from anywhere using singletone instance

    public BannerView RequestBanner(BannerView bannerView)
    {
    #if UNITY_ANDROID
    string adUnitId = androidBannerAddID;
    #elif UNITY_IPHONE
    string adUnitId = "ca-app-pub-3940256099942544/2934735716";
    #else
    string adUnitId = "unexpected_platform";
    #endif

    //Create a Banner......
    if (bannerView != null)
    {
    bannerView.Hide();
    bannerView.Destroy();
    }

    //bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
    bannerView = new BannerView(adUnitId, AdSize.SmartBanner,0,h);
    // Register for ad events.
    //bannerView.OnAdLoaded += HandleAdLoaded;
    //bannerView.OnAdFailedToLoad +=HandleAdFailedToLoad;
    //bannerView.OnAdOpening +=HandleAdOpened;
    //bannerView.OnAdClosed +=HandleAdClosed;
    //bannerView.OnAdLeavingApplication +=HandleAdLeftApplication;
    // Load a banner ad.
    bannerView.LoadAd(CreateAdRequest());
    return bannerView;

    }

    -for destroying that object call this method and pass the bannerview object to be destroy..


    public void DestroyBanner(BannerView bannerView)
    {

    if (bannerView != null)
    {
    bannerView.Hide();
    bannerView.Destroy();
    }

    }
     
  22. vignesh211

    vignesh211

    Joined:
    Feb 8, 2019
    Posts:
    13
    Currently had issues like you.

    I think you have to destroy the ad before loading the other scene.

    This worked for me.
     
  23. gun_man

    gun_man

    Joined:
    Sep 19, 2020
    Posts:
    16
    Keep in mind to keep admob banner on scene minimum 60 second or you get invalid request or something and make your admob account limited.