Here is my AdManager.cs:
public class AdManager : MonoBehaviour {
public static AdManager Instance { set; get; }
public string bannerID;
public string videoID;
private BannerView bannerView;
private InterstitialAd interstitial;
private AdRequest request;
private void Start () {
Instance = this;
//DontDestroyOnLoad (gameObject);
#if UNITY_EDITOR
#elif UNITY_ANDROID
MobileAds.Initialize(bannerID);
MobileAds.Initialize(videoID);
interstitial = new InterstitialAd(videoID);
request = new AdRequest.Builder().Build();
interstitial.LoadAd(request);
#endif
}
public void ShowBanner(){
#if UNITY_EDITOR
Debug.Log("Unable to play ads from EDITOR");
#elif UNITY_ANDROID
bannerView = new BannerView(bannerID, AdSize.Banner, AdPosition.Bottom);
request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
#endif
}
public void ShowVideo(){
#if UNITY_EDITOR
Debug.Log("Unable to play ads from EDITOR");
#elif UNITY_ANDROID
if (interstitial.IsLoaded()) {
interstitial.Show();
}
#endif
}
public void DestroyAd()
{
Debug.Log("Ads Hided");
bannerView.Hide ();
}
}
Here is the Start() function of a script called CarSpawner.cs:
void Start () {
Image[] car = new Image[4];
timer = delayTimer;
gscore.text = "Score " + score;
bestscore.text = "Best " + PlayerPrefs.GetInt ("BestScore", 0).ToString();
if (PlayerPrefs.GetInt ("Rules", 0) != 1) {
check = 1;
rules.SetActive (true);
StartCoroutine (ExecuteMessage (6));
}
AdManager.Instance.DestroyAd ();
}
I am getting an error at AdManager.Instance.DestroyAd();
in the CarSpawner.cs
script. The following is the error that I am getting when I play the game:
NullReferenceException: Object reference not set to an instance of an object
AdManager.DestroyAd() (at Assets/Scripts/AdManager.cs:51)
CarSpawner.Start () (at Assets/Scripts/CarSpawner.cs:97)