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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How do I keep a reference to a class on Application.LoadLevel (Application.loadedLevel). Admob

Discussion in 'Scripting' started by Regnav, Aug 12, 2015.

  1. Regnav

    Regnav

    Joined:
    Jul 13, 2015
    Posts:
    2
    Hello,

    So my game has very short levels, could be from 1 sec to 30 sec and my ads get reloaded everytime I reload the scene.

    What I want to do is when you die I show an ad, but do not want to make a Adrequest every 5 seconds, but rather just show it.

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

    public class GoogleAdmob : MonoBehaviour
    {
    private BannerView bannerView;
    private static string outputMessage = "";
    private static bool created = false;

    public static string OutputMessage
    {
    set { outputMessage = value; }
    }


    void Awake(){
    if(!created){
    DontDestroyOnLoad(gameObject);
    created = true;
    } else {
    Destroy(gameObject);
    }
    }

    void Start(){
    RequestBanner ();
    bannerView.Hide ();
    }

    public void RequestBanner()
    {
    #if UNITY_EDITOR
    string adUnitId = "unused";
    #elif UNITY_ANDROID
    string adUnitId = "xxx";
    #elif UNITY_IPHONE
    string adUnitId = "xxx";
    #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());
    }

    // Returns an ad request with custom ad targeting.
    private AdRequest createAdRequest()
    {
    return new AdRequest.Builder()
    //.AddTestDevice(AdRequest.TestDeviceSimulator)
    //.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
    .AddKeyword("game")
    //.SetGender(Gender.Male)
    //.SetBirthday(new DateTime(1985, 1, 1))
    //.AddExtra("color_bg", "9B30FF")
    .Build();

    }

    }​

    But whenever the scene is reloaded, i lose the reference to the bannerView, and I can no longer cal:
    bannerView.Show();
    bannerView.Hide();

    How can I keep the reference on reload?
     
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    I would say call DontDestroyOnLoad but you already seem to be doing that. You should make sure "created" equals false on Awake. Make sure RequestBanner is being called as well...
     
  3. Regnav

    Regnav

    Joined:
    Jul 13, 2015
    Posts:
    2
    Thank you for your reply, I am calling RequestBanner in Start(), also "created" is false on startup. The ploblem lies in the fact that When the scene is reloaded, the reference to bannerView is lost..
     
  4. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    That's strange.. Could be the fact that maybe the API is being reloaded... Other than that I don't know sorry D: I would try some Debug statements, and maybe try some public variables instead.