0
\$\begingroup\$

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.

enter image description here

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;

// AdMob
using GoogleMobileAds.Api;

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>();

    // AdMob Ad
    RequestBanner ();
  }

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

  // Back to Main Menu
  public void Back_To_Main_Menu(string Main_Menu)
  {
    //Playing Audio
    My_Audio.PlayOneShot(My_Audio_Clip);

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

    Application.LoadLevel (Main_Menu);
  }

  // AdMob Advertisement
  private void RequestBanner()
  {
    #if UNITY_ANDROID
    string adUnitId = "ca-app-pub-xxxxxxxx/xxxxxxxx";
    #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 bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
    // Create an empty ad request.
    AdRequest request = new AdRequest.Builder().Build();
    // Load the banner with the request.
    bannerView.LoadAd(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.

\$\endgroup\$
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Alexandre Vaillancourt Jan 20 '16 at 19:58
1
\$\begingroup\$

You have an issue with the bannerView variable scope.

Instead of writing

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

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

using UnityEngine;
using System.Collections;

// AdMob
using GoogleMobileAds.Api;

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);
\$\endgroup\$
  • \$\begingroup\$ mBannerView and bannerView aren't the same. \$\endgroup\$ – Jesse Williams Jan 20 '16 at 19:27
  • 1
    \$\begingroup\$ @JesseWilliams fixed :) my bad habbits! \$\endgroup\$ – Alexandre Vaillancourt Jan 20 '16 at 19:29
1
\$\begingroup\$

Remove Banner

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

Admob.Instance().removeBanner();

ref https://github.com/unity-plugins/Unity-Admob#3remove-banner

\$\endgroup\$
-1
\$\begingroup\$

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;

// AdMob
using GoogleMobileAds.Api;
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.

\$\endgroup\$
  • \$\begingroup\$ That's declaring the variable in the global scope :) \$\endgroup\$ – Alexandre Vaillancourt Jan 20 '16 at 19:29
  • \$\begingroup\$ Bah, oops! lol Well, it would still work, but may not be ideal. Otherwise it looks like we have the same answer. \$\endgroup\$ – Jesse Williams Jan 20 '16 at 19:31
  • \$\begingroup\$ 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. \$\endgroup\$ – Jesse Williams Jan 20 '16 at 19:33
  • \$\begingroup\$ @JesseWilliams , It Gives Error -- 'error CS0116: A namespace can only contain types and namespace declarations' \$\endgroup\$ – Nicolus Buck Jan 20 '16 at 19:56
  • \$\begingroup\$ Hmmm, then go with Alexandre's scoping. It's better form anyhow. People HATE globally scoping things lol. Oops! \$\endgroup\$ – Jesse Williams Jan 20 '16 at 19:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.