Search Unity

AdMob Show & Hide Banner Upon Screen Change - Calling another function from another script

Discussion in 'Getting Started' started by SynGT, Mar 28, 2016.

  1. SynGT

    SynGT

    Joined:
    Mar 15, 2016
    Posts:
    2
    Hi -

    I have my AdMobs script which places the banners, which places the ads just fine. I'm trying to hide them when the scene changes & then unhide them on another scene change. Essentially, each scene change has a bit of script that I'm trying to tuck a call to a function in the main AdMobs script, to hide/unhide. The problem is, it doesn't seem to be working.

    Here is my AdMobs Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using GoogleMobileAds;
    4. using GoogleMobileAds.Api;
    5. using UnityEngine.SceneManagement;
    6.  
    7.  
    8. public class RequestAdMob : MonoBehaviour {
    9.  
    10.     public BannerView bannerView;
    11.  
    12.     void Start() {
    13.  
    14.  
    15.  
    16.         #if UNITY_ANDROID
    17.         string adUnitId = "ca-app-pub-xxxxxxxxxx";
    18.         #elif UNITY_IPHONE
    19.         string adUnitId = "ca-app-pub-xxxxxxxxxx";
    20.         #else
    21.         string adUnityId = "unexpected_platform";
    22.         #endif
    23.  
    24.            
    25.         Debug.Log (bannerView);
    26.         bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Top);
    27.         AdRequest request = new AdRequest.Builder ().Build ();
    28.         bannerView.LoadAd (request);
    29.         bannerView.Show ();
    30.  
    31.  
    32.     }
    33.  
    34.     public void HideBans() {
    35.         Debug.Log ("Hide");
    36.         bannerView.Hide();
    37.         Debug.Log ("Hide");
    38.     }
    39.  
    40.     public void ShowBans() {
    41.         bannerView.Show();
    42.         Debug.Log ("Show");
    43.     }
    44. }
    45.  
    Then I have a simple script I attached to the main camera to hide the banner on the next scene change:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShowBans : MonoBehaviour {
    5.  
    6.     public RequestAdMob rAM;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.         rAM = GetComponent<RequestAdMob>();
    12.         rAM.HideBans();
    13.  
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.    
    19.     }
    20. }
    21.  
    But I'm apparently not the brightest & am missing something.

    Any help would be appreciated.
     
  2. pat_sommer

    pat_sommer

    Joined:
    Jun 28, 2010
    Posts:
    586
    which part is not working? The hiding when leaving the scene, or showing when loading the scene?

    If your ShowBans class is on the main camera of the new scene, you essentially have a race condition of which Start method will be called first, creating your banner, or hiding your banner.

    if your hidebans, is called after you create your banner, it will be hidden.

    im trying to do a similar thing. but, im hiding the banner before the scene change, and i have my banner as a static object so the reference persists.

    essentially im doing :
    - hide banner in scene 1
    - load scene 2
    - show banner in scene 2

    this is working for me. However, i see another problem with hiding a banner. even though its not visible, it still blocks touch events, and i cant touch UI items below the "area" where the banner would be. are you experiencing this?