NullReferenceException error, can't identify why

I’m following a tutorial at the moment on how to make a space shooter game. I’ve followed a Unity tutorial previously before years ago, and done various courses on Codecademy, so I’m not 100% a stranger to coding, but an error’s popped up on my game that hasn’t on the tutorial. Sorry if my terminology isn’t properly correct in what I’m explaining:

Basically, I’m calling a method from another script - to do this, I’m assigning that script component to a variable in Start and then calling thatVariable.Method(), so it looks like this (I’ve cut out anything not really relevant).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    private GameManager _gameManager;
 
    // Start is called before the first frame update
    void Start()
    {
        _gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();

       if (_gameManager = null)
      {
          Debug.LogError("The Game Manager is NULL.");
      }
    }

    public void UpdateLives(int currentLives)
    {
        if (currentLives < 1)
        {
            Debug.Log("Player is dead");
            GameOverSequence();
        }
    }

    private void GameOverSequence()
    {
        _gameManager.GameOver();
    }

When I type it like this, the code all compiles and I can run the game, but upon the player losing their last life, I get a NullReferenceException error pop up referencing 4 lines of codes each from different scripts, the player isn’t destroyed, and enemies and powerups keep spawning (of which they are all controlled through different scripts). However, if I don’t create and assign the variable, and instead just type it as

private void GameOverSequence()
    {
        GameObject.Find("GameManager").GetComponent<GameManager>().GameOver();
    }

then everything works fine. I’m not sure if the tutorial is going to require much more communication between the UIManager and GameManager scripts so I’d like to try and get it to work the first way. Can anyone suggest why it might be erroring out just from being used in a variable?

Seems like it’s probably a timing issue. GameManager doesn’t exist when Start is called, but by the time you’re running GameOverSequence, it has been created. Are both of these objects in the scene from the beginning? It seems like not. How and when are you creating the GameManager object?

GameObject.Find is always bad! A singleton pattern (briefly described in that link) would be much better for this scenario, and you wouldn’t have this sort of execution-order issue.

Also just took a second look at your code, and on line 15 you’re setting _gameManager to null within that if statement. That should be ==, not =. That’s your actual problem.

Still would be better off as a singleton anyway.

2 Likes

The GameManager GameObject is in the scene even before starting the game. The _gameManager variable in the UIManager script is declared in the class and then assigned in Start().

Seems like this was it! Such a small mistake, I was under the impression = failed within if logic checks for some reason so wasn’t even checking. Thank you!

1 Like