NullReferenceException: Object reference not set to an instance of an object
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 ();
}
}
}
Answer by jgodfrey · Jun 06, 2016 at 10:03 PM
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);
Answer by eruizr · Jun 07, 2016 at 11:29 AM
Thank you very much. Now it works perfectly.
NullReferenceException: Object reference not set to an instance of an object chap2.Game$$anonymous$$anager_Toggle$$anonymous$$enu.Toggle$$anonymous$$enu () (at Assets/$$anonymous$$y script/Chapter2/Game$$anonymous$$anager Script/Game$$anonymous$$anager_Toggle$$anonymous$$enu.cs:52) chap2.Game$$anonymous$$anager_Toggle$$anonymous$$enu.Start () (at Assets/$$anonymous$$y script/Chapter2/Game$$anonymous$$anager Script/Game$$anonymous$$anager_Toggle$$anonymous$$enu.cs:15)
There is my game script..down using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace chap2 { public class Game$$anonymous$$anager_TogglePause : $$anonymous$$onoBehaviour {
private Game$$anonymous$$anager_$$anonymous$$aster game$$anonymous$$anager$$anonymous$$aster;
private bool isPaused;
// Use this for initialization
void OnEnable ()
{
SetInitialReferences ();
game$$anonymous$$anager$$anonymous$$aster.$$anonymous$$enuToggleEvent += TogglePause;
game$$anonymous$$anager$$anonymous$$aster.InventoryUIToggleEvent += TogglePause;
}
// Update is called once per frame
void OnDisable ()
{
game$$anonymous$$anager$$anonymous$$aster.$$anonymous$$enuToggleEvent -= TogglePause;
game$$anonymous$$anager$$anonymous$$aster.InventoryUIToggleEvent -= TogglePause;
}
void SetInitialReferences()
{
game$$anonymous$$anager$$anonymous$$aster = GetComponent<Game$$anonymous$$anager_$$anonymous$$aster> ();
}
void TogglePause()
{
if (isPaused) {
Time.timeScale = 1;
isPaused = false;
}
else
{
// Time.timeScale = 0; isPaused = true; } } }
}
and the other script is here using System.Collections; using System.Collections.Generic; using UnityEngine;
namespace chap2 { public class Game$$anonymous$$anager_Toggle$$anonymous$$enu : $$anonymous$$onoBehaviour { private Game$$anonymous$$anager_$$anonymous$$aster game$$anonymous$$anager$$anonymous$$aster; public GameObject menu;
// Use this for initialization
void Start ()
{
Toggle$$anonymous$$enu ();
}
// Update is called once per frame
void Update ()
{
CheckFor$$anonymous$$enuToggleRequest ();
}
void OnEnabled()
{
SetInitialReferences ();
game$$anonymous$$anager$$anonymous$$aster.GameOverEvent += Toggle$$anonymous$$enu;
}
void OnDisabled()
{
game$$anonymous$$anager$$anonymous$$aster.GameOverEvent -= Toggle$$anonymous$$enu;
}
void SetInitialReferences()
{
game$$anonymous$$anager$$anonymous$$aster = GetComponent<Game$$anonymous$$anager_$$anonymous$$aster> ();
}
void CheckFor$$anonymous$$enuToggleRequest()
{
if (Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Escape) && !game$$anonymous$$anager$$anonymous$$aster.isGameOver && !game$$anonymous$$anager$$anonymous$$aster.isInventoryUIOn)
{
Toggle$$anonymous$$enu ();
}
}
void Toggle$$anonymous$$enu()
{
if (menu != null) {
menu.SetActive (!menu.activeSelf);
game$$anonymous$$anager$$anonymous$$aster.is$$anonymous$$enuOn = !game$$anonymous$$anager$$anonymous$$aster.is$$anonymous$$enuOn;
game$$anonymous$$anager$$anonymous$$aster.CallEvent$$anonymous$$enuToggle ();
}
else
{
Debug.LogWarning("You need to assign a UI GameObject to the Toggle $$anonymous$$enu script in the inspector.");
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612082406im_/https://answers.unity.com/themes/thub/images/avi.jpg)