Unable to call bannerView.Hide(); from a scene in Unity I don't want the ad to show

3,607 views
Skip to first unread message

Dhaval Soni

unread,
Jun 16, 2014, 2:48:29 PM6/16/14
to google-adm...@googlegroups.com
I'm using the version 2.1 of the plugin in Unity and it works just fine; but due to my lack of programming skills, I'm unable to understand how to call the bannerView.Hide(); in the Scene02 of my game. In the Scene01, I have already requested banner and shown banner successfully by attaching the script to the Camera, but I want this banner to be hidden in Scene02. How do I achieve this ?  The below is the code that I used from your git

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

// Example script showing how to invoke the Google Mobile Ads Unity plugin.
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
private BannerView bannerView;
private InterstitialAd interstitial;

void Start()
{
DontDestroyOnLoad(this);
RequestBanner ();
RequestInterstitial ();
bannerView.Show ();
}
private void RequestBanner()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
// Register for ad events.
bannerView.AdLoaded += HandleAdLoaded;
bannerView.AdFailedToLoad += HandleAdFailedToLoad;
bannerView.AdOpened += HandleAdOpened;
bannerView.AdClosing += HandleAdClosing;
bannerView.AdClosed += HandleAdClosed;
bannerView.AdLeftApplication += HandleAdLeftApplication;
// Load a banner ad.
bannerView.LoadAd(createAdRequest());
}
private void RequestInterstitial()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
interstitial.AdLoaded += HandleInterstitialLoaded;
interstitial.AdFailedToLoad += HandleInterstitialFailedToLoad;
interstitial.AdOpened += HandleInterstitialOpened;
interstitial.AdClosing += HandleInterstitialClosing;
interstitial.AdClosed += HandleInterstitialClosed;
interstitial.AdLeftApplication += HandleInterstitialLeftApplication;
// Load an interstitial ad.
interstitial.LoadAd(createAdRequest());
}
// Returns an ad request with custom ad targeting.
private AdRequest createAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("9ACD01B470951161A30C0AA4B6DA3A7D")
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
private void ShowInterstitial()
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
print("Interstitial is not ready yet.");
}
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
print("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
print("HandleAdOpened event received");
}
void HandleAdClosing(object sender, EventArgs args)
{
print("HandleAdClosing event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
print("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
print("HandleInterstitialOpened event received");
}
void HandleInterstitialClosing(object sender, EventArgs args)
{
print("HandleInterstitialClosing event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
print("HandleInterstitialClosed event received");
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
print("HandleInterstitialLeftApplication event received");
}
#endregion
}

Unul Individ

unread,
Jun 17, 2014, 10:08:51 AM6/17/14
to google-adm...@googlegroups.com
    I don't know exactly if banners do show during scene loading, but you can check which scene you are on with Application.loadedLevel : http://docs.unity3d.com/ScriptReference/Application-loadedLevel.html ... and hiding the banner would look like this :
• if you load the banner from the Start method of an object, you can simply verify if Application.loadedLevel == the indice you take from the scene list in the build window and show the banner only if it is false
    - you can also use Application.loadedLevelName : http://docs.unity3d.com/ScriptReference/Application-loadedLevelName.html
• if the banner stays through levels (I am not sure because I have not attached any script to some gameobject), then in the Start function you make one of the above checks and call bannerView.Hide()

    I hope I covered your actual problem.
    Good luck :)

Dhaval Soni

unread,
Jun 17, 2014, 11:52:55 AM6/17/14
to google-adm...@googlegroups.com
Hi,
Thanks a bunch for taking the time to reply to my query, however, I'm still having problems hiding the banner during the Game level. I've tried to check for the level indice as well as the level name string but neither manage to hide the banner. Below is the code I added under the Start function:

 if (Application.loadedLevel == 2) {
     bannerView
.Hide ();
 
}
 
if (Application.loadedLevelName == "Game") {
     bannerView
.Hide ();
 
}
 I also tried adding it under the Update function but to no avail. Just to be clear, I've added the script from the first post, onto the camera of the MAIN MENU (not the GAME), which now also contains, this newly added code. Since I'm using DoNotDestroyOnLoad(this); , I'm assuming that this script does not get destroyed when the Game level is loaded, and hence should continue to check against the level name/indice to make the banner hidden. My question here is, do I also need to add the script from the first post, into the Game level, and check against the level name/indice and hide it ? I apologize if I sound terribly confused noob here, since it is my first time dealing with such intense code.
Thanks yet again !

Dhaval Soni

unread,
Jun 17, 2014, 1:19:51 PM6/17/14
to google-adm...@googlegroups.com
Update: I added the script with the loaded level indice/name check in the Game level only, instead of the Main Menu, and the banner does stay hidden under the condition, and visible when no such condition exists, so I can confirm that the level name check works perfectly, as you suggested. But, the problem still persists - that I'm unable to control the toggle of the banner visibility in a different scene. 

Unul Individ

unread,
Jun 18, 2014, 9:53:10 AM6/18/14
to google-adm...@googlegroups.com
    If you have marked the Main Camera with DontDestroyOnLoad, the check in the Start function is useless; rather, when you load the level, you can make the check there (assuming you load the levels from the camera-attached script) and hide the banner (or better, destroy it and create another one in the scenes you want banners in).
    The problem you describe seems very odd, though. Not hiding the banner when you make that check sounds pretty confusing. Try as I wrote above - Show() / Hide() the banner right before/after you load the next level.

Dhaval Soni

unread,
Jun 18, 2014, 12:11:11 PM6/18/14
to google-adm...@googlegroups.com
Anyone have a solution to this ? Kindly share your suggestions.

Eric Leichtenschlag

unread,
Jun 18, 2014, 4:27:24 PM6/18/14
to google-adm...@googlegroups.com
Hi Dhaval,

One option would be to call bannerView.Destroy() before you leave the first scene, and create a new banner in the second scene. The downside here is that every scene change means requesting a new banner.

I haven't tried using multiple scenes in Unity before but it seems like you would just need some handle to the bannerView instance in your game level scene.

Thanks,
Eric

Dhaval Soni

unread,
Jun 19, 2014, 11:39:55 PM6/19/14
to google-adm...@googlegroups.com
Hi Eric,
It doesn't bode well for my game to request an ad banner and destroy it each time. By the time a banner might load, the Player might already be on a different scene and hence doesn't translate to good monetizing practices in my opinion. The ads should ideally be ready to load and toggling the visibility on or off is the best way to go. It would be really awesome if there was a generic function that could be called from ANY scene so as to hide/show any banner that is currently loaded. If it isn't too much to ask for, could you kindly look into this ?
Thanks
Dhaval

Eric Leichtenschlag

unread,
Jun 20, 2014, 2:00:29 PM6/20/14
to google-adm...@googlegroups.com
Can you send an example of an app with two scenes, and how you'd maintain a reference to the bannerView across multiple scenes? It would help me figure out what the plugin can do to help on that front.

Thanks,
Eric

Dhaval Soni

unread,
Jun 20, 2014, 3:06:47 PM6/20/14
to google-adm...@googlegroups.com
I've made a Unity package of the entire test project. As you can see that I can successfully call and load the ads in the Main Menu, I'm unable to hide them in the Game scene. What function should I call, in the Game scene, so as to hide any ad that has been loaded ? As previously discussed, destroying the ad in the Main Menu as isn't a favorable option as I'd like to call the same banner ad in the GameOver screen, so as to reduce the number of requests for fresh ads from AdMob. Massive thanks for volunteering to help me with my issue here. Here is the link to the entire Unity project - https://dl.dropboxusercontent.com/u/3010784/AdMobTestProject.unitypackage

Eric Leichtenschlag

unread,
Jun 20, 2014, 7:16:09 PM6/20/14
to google-adm...@googlegroups.com
I played around with your sample too bit and so I think it'll be easier to explain what I changed than to send a .unitypackage back.

Since you need to hold a reference to a banner between scenes, we'll create a special GameObject to hold on to, and attach ads related behaviors to that GameObject.

// MainMenuAdControl.cs
void Start() {      
   // Create an empty GameObject to hold the banner. Attach ads behavior to it, and don't destroy it when new scenes load.
   GameObject myGameObject = new GameObject("myGameObject");
   myGameObject.AddComponent<BannerControl>();
   DontDestroyOnLoad(myGameObject);
}

My BannerControl.cs looks like this:

// BannerControl.cs

using
System;

using UnityEngine;
using GoogleMobileAds;
using GoogleMobileAds.Api;

public class BannerControl : MonoBehaviour {
   
    public BannerView bannerView;
   
    void Start()
    {
        bannerView = new BannerView("your_ad_unit_id", AdSize.SmartBanner, AdPosition.Bottom);
        AdRequest request = new AdRequest.Builder().Build();
        bannerView.LoadAd (request);
       
        bannerView.Show();
    }
}


Then in your GameAdControl.cs, which is attached to the second scene, find the GameObject, get the BannerControl component, and you have access to the BannerView:

// GameAdControl.cs

void Start () {
    GameObject myGameObject = GameObject.Find("myGameObject");
    BannerControl bannerControl = myGameObject.GetComponent<BannerControl>();
    bannerControl.bannerView.Hide();
}


From a Unity perspective, it's probably better to have a different GameObject just for ads, so you're only holding onto that GameObject through scenes, and not the entire main menu GameObject.

Hope this helps,
Eric

Dhaval Soni

unread,
Jun 22, 2014, 2:51:11 PM6/22/14
to google-adm...@googlegroups.com
Hi Eric,
I understood what my mistake was. I wasn't accessing the gameobject that held the banner control script, and hence could not call the hide banner function. Thanks to you, it all made sense in the end. Thanks a lot for getting into this issue for me and explaining what I was doing wrong. I'm certain I learnt a few new things in my pursuit to get AdMob working in my game. You can mark this thread 'Solved'. 
Thanks to everyone who pitched in.
Dhaval

Abhishek

unread,
Dec 30, 2014, 11:17:30 AM12/30/14
to google-adm...@googlegroups.com
Hi Eric/Dhaval,

i have been facing the same issue regarding showing the ads.
my question is: In the second scene what do i add the GameAdControl.cs script to? Do i create a new game object in that scene and attach it to that? i have already created a gameobject in my main menu scene to which i am attaching the initial scripts?
Any help would be greatly appreciated.
Thanks in advance!!!

-Abhishek

Gp Sidhu

unread,
Jan 21, 2015, 10:34:18 AM1/21/15
to google-adm...@googlegroups.com
Guys  the solution here is very simple ..

Say in scene 1 you load an AD .. Call   OnDestroy()  method to hide the ad when you leave that particular scene

khalil khan

unread,
Aug 21, 2015, 6:51:53 AM8/21/15
to Google Mobile Ads SDK Developers
Hey Eric It Work But only one time Not every Time I mean when start game it work but when game over and restart same scene it again show ad .. So What we Next do?

Jason Wightman

unread,
Sep 18, 2015, 9:03:34 AM9/18/15
to Google Mobile Ads SDK Developers
khalil, I know this an old board, but for people like me who found this from google, the reason that it keeps showing up and only destroys or hides it the first time is because the "myGameObject" keeps cloning itself each time you go back to the main menu. to fix it; on the "GameAdControl" script add "Destroy (myGameObject);"at the end of "void Start" and it will work perfectly.

FYI: this works perfectly with Eric Leichtenschlag's answer earlier on this board. 

Elton Kent

unread,
Sep 18, 2015, 5:13:56 PM9/18/15
to Google Mobile Ads SDK Developers
Hi Jason,
Thanks for posting your fix. Glad you found Eric's answer useful.

Cheers,
Elton Kent
Mobile Ads SDK Team
Reply all
Reply to author
Forward
0 new messages