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

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?

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…

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…

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.