I am currently working on a small game project and I want to monetize my game with AdMob ads. So I watched this YouTube Tutorial.

It works fine, but the problem is ad appears in all the scenes. So I don't want that. I want the ads on certain scenes.

For example, look at the image below.

I want the ad on Instruction scene. So when the player clicks on Back To Main Menu button ad should disappear.

I tried

bannerView.Hide ();
bannerView.Destroy();


but Unity gives some errors, it says:

error CS0103: The name bannerView does not exist in the current context


Here is the script:

using UnityEngine;
using System.Collections;

public class Instruction_Level_Manager_Script : MonoBehaviour
{
// Audio
public AudioClip My_Audio_Clip ;
private AudioSource My_Audio ;

// Use this for initialization
void Start ()
{
My_Audio = GetComponent<AudioSource>();

RequestBanner ();
}

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

{
//Playing Audio
My_Audio.PlayOneShot(My_Audio_Clip);

//Destroy when leaving the level
bannerView.Hide ();
bannerView.Destroy();

}

private void RequestBanner()
{
#if UNITY_ANDROID
#elif UNITY_IPHONE
#else
#endif

// Create a 320x50 banner at the top of the screen.
// Create an empty ad request.
// Load the banner with the request.
}
}


So what is wrong with this script? I Googled it, but couldn't find satisfying solution. If this method is not possible, how can I destroy the ad?

I am using Unity 5.

• No problem! Don't forget to accept the answers that proved to be solutions to your questions and to upvote those that have been useful to you :) – Vaillancourt Jan 20 '16 at 19:58

You have an issue with the bannerView variable scope.

BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);


in the RequestBanner method, do it in the class scope:

using UnityEngine;
using System.Collections;

public class Instruction_Level_Manager_Script : MonoBehaviour
{
// Audio
public AudioClip My_Audio_Clip ;
private AudioSource My_Audio ;

private BannerView bannerView; // Put this here
.
.
.


and in the RequestBanner mtehod, simply do:

bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);

• mBannerView and bannerView aren't the same. – Jesse Williams Jan 20 '16 at 19:27
• @JesseWilliams fixed :) my bad habbits! – Vaillancourt Jan 20 '16 at 19:29

Remove Banner

By default, banners are visible. To temporarily hide a banner, call:

Admob.Instance().removeBanner();


bannerView only exists in the context of RequestBanner() and does not exist in the context of BackToMainMenu().

You can make bannerview available by declaring it in the outer scope like this:

using UnityEngine;
using System.Collections;

private BannerView bannerView;

public class Instruction_Level_Manager_Script : MonoBehaviour


then in RequestBanner() changing your line to:

bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);


Scope is everything.

• That's declaring the variable in the global scope :) – Vaillancourt Jan 20 '16 at 19:29
• Bah, oops! lol Well, it would still work, but may not be ideal. Otherwise it looks like we have the same answer. – Jesse Williams Jan 20 '16 at 19:31
• Actually, one reason you may want to assign it in global, and possibly make it public, is if you want to create a child object prefab to attach the banner to, and just drop it in as the object. It would take a bit more work as you'd need to reference the component from the child object, but it might make it easier to hide and display it. – Jesse Williams Jan 20 '16 at 19:33
• @JesseWilliams , It Gives Error -- 'error CS0116: A namespace can only contain types and namespace declarations' – Nicolus Buck Jan 20 '16 at 19:56
• Hmmm, then go with Alexandre's scoping. It's better form anyhow. People HATE globally scoping things lol. Oops! – Jesse Williams Jan 20 '16 at 19:58