0

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)

Igor
  • 55,253
  • 10
  • 80
  • 149
Aravind
  • 11
  • 1
  • 10
  • 2
    `AdManager` instance only gets assigned when the `AdManager.cs` script `Start()` method is executed. If `CarSpawner.cs` is Started first, the instance won't exist. You should assign the `AdManager` instance in the `Awake()` method or force the order of the scripts. – jiveturkey Sep 20 '18 at 18:18
  • Do you run Unity, see your banner and then your program crashes? Or do you see the banner in one build, but a crash in a different build of your program? –  Sep 20 '18 at 18:19
  • Looks like `bannerView` is `null` (see stack trace in the exception). This can happen if you never assign it an instance. If you don't care if there is an instance then add the Safe Navigation operator. `bannerView?.Hide();` – Igor Sep 20 '18 at 18:22
  • @jiveturkey Thank you for the quick response. I will try that. – Aravind Sep 20 '18 at 18:24
  • Let me know how it goes – jiveturkey Sep 20 '18 at 18:26
  • @Igor I also tried creating an object "AdManager adm;" in the CarSpawner.cs script and tried adm.bannerView.Hide(); . But it gave me an error. – Aravind Sep 20 '18 at 18:29
  • @Aravind - i am not sure what that has to do with my comment above... – Igor Sep 20 '18 at 18:30
  • @Igor I don't know what you meant by bannerView?.Hide();. Anyway that '?' is giving me an error. – Aravind Sep 20 '18 at 18:33
  • @Aravind - it's supported in a version of `c#` that you are not using (*we know that based of the error but no way for me to know before that*). It translates into `if(bannerView != null) bannerView.Hide();` – Igor Sep 20 '18 at 18:34
  • @Igor That solved the issue! Thanks a lot buddy!! :)) If you can write it as an answer, I will accept it as an answer – Aravind Sep 20 '18 at 18:38
  • @Aravind - Your question is a duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/q/4660142/1260204) and is marked as such. Questions marked as duplicates should not have an additional answer. The answer provided in that link is very detailed and good, considering NREs (**N**ull**R**eference**E**xceptions) are the most frequently occurring exception type it is worth reading over. – Igor Sep 20 '18 at 18:39
  • @Igor Okay. Thanks for the help anyway! Have a good day! – Aravind Sep 20 '18 at 18:43

0 Answers0