NullReferenceException: Object reference not set to an instance of an object (166839)

Hello everyone, I´m new in unity 3d and I have encountered a problem when trying to show an interstitial ad when the game is over.

I find the following error:
“NullReferenceException: Object reference not set to an instance of an object
GameManager.ShowAd () (at Assets/Scripts/GameManager.cs:114)
GameManager.FinJuegoEnemy () (at Assets/Scripts/GameManager.cs:63)”

I tried a lot of things, but I can find the problem. Please , can you help me?

Thank you very much in advance.

Here is the code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using GoogleMobileAds.Api;

public class GameManager : MonoBehaviour
{
	public float startDelay;
	public static GameManager instance = null;
    
	public GameObject canvasIntro;
	public GameObject canvasMenu;
	public GameObject gameBoard;
	public GameObject canvasOverEnd;
	Text textEnd;

	InterstitialAd interstitial;
	private BannerView bannerView;

	void Awake ()
	{
		//Check if instance already exists
		if (instance == null) {
			//if not, set instance to this
			instance = this;
		}
		//If instance already exists and it's not this:
		else if (instance != this) {
			//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
			Destroy (gameObject);
		}
		//Sets this to not be destroyed when reloading scene
		DontDestroyOnLoad (gameObject);
		canvasIntro.SetActive (true);
		RequestBanner ();
		RequestInterstitial ();
		Invoke ("startMenu", startDelay);
	}

	void startMenu ()
	{
		canvasIntro.SetActive (false);
		canvasMenu.SetActive (true);
	}

	public void FinJuegoPlayer ()
	{
		gameBoard.SetActive (false);
		canvasOverEnd.SetActive (true);
		textEnd = GameObject.Find ("TextWinLoose").GetComponent<Text> ();
		textEnd.text = "You Win!";
		ShowAd ();
	}

	public void FinJuegoEnemy ()
	{
		gameBoard.SetActive (false);
		canvasOverEnd.SetActive (true);
		textEnd = GameObject.Find ("TextWinLoose").GetComponent<Text> ();
		textEnd.text = "Sorry, you lost!";
		ShowAd ();
	}

	public void salir ()
	{
		Application.Quit ();
	}

	private void RequestBanner ()
	{
		#if UNITY_ANDROID
		string adUnitId = "ca-app-pub-4526259028819910/8305328189";
		#elif UNITY_IPHONE
		string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
		#else
		string adUnitId = "unexpected_platform";
		#endif

		// Create a 320x50 banner at the bottom of the screen.
		BannerView bannerView = new BannerView (adUnitId, AdSize.Banner, AdPosition.Bottom);
		// Create an empty ad request.
		AdRequest request = new AdRequest.Builder ().Build ();
		// Load the banner with the request.
		bannerView.LoadAd (request);
	}

	private void RequestInterstitial ()
	{
		#if UNITY_ANDROID
		string adUnitId = "ca-app-pub-4526259028819910/6688994189";
		#elif UNITY_IPHONE
		string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
		#else
		string adUnitId = "unexpected_platform";
		#endif

		// Initialize an InterstitialAd.
		InterstitialAd interstitial = new InterstitialAd (adUnitId);
		// Create an empty ad request.
		AdRequest request = new AdRequest.Builder ().Build ();
		// Load the interstitial with the request.
		interstitial.LoadAd (request);
	}

	public void HideBanner ()
	{
		bannerView.Hide ();
	}

	public void ShowAd ()
	{
		if (interstitial.IsLoaded ()) {
			interstitial.Show ();
		}
	}
}

Your problem is likely here:

// Initialize an InterstitialAd.
InterstitialAd interstitial = new InterstitialAd (adUnitId);

You’ve already declared a class-level variable named “interstitial” of type InterstitialAd near the top of the posted code. I assume that’s the variable you’re trying assign an object to in the above code. However, since you redeclare the variable type above, that’s a brand new, local (and completely different) variable than the one declared at the top.

Since the global variable was never assigned a value (it was assigned to a different, local variable), it’s null when you try to reference it in the ShowAd() method.

To fix it, just drop the type in the above code. So…

// Initialize an InterstitialAd.
interstitial = new InterstitialAd (adUnitId);

Thank you very much. Now it works perfectly.

You request ads before Mobile ads. initialized.