Object reference not set to an instance of an object

Hey there me again…
I am getting the error of
NullReferenceException: Object reference not set to an instance of an object. It says its in the GameManager line 20.

And then the same again on Line 26 but it spams this thousands of times!
due to the error none of the canvas gets enabled or disabled which ever it needs to do to.

public class GameManager : MonoBehaviour
{

    Quiz quiz;
    EndScreen endScreen;

    void Awake()
    {
        quiz = FindObjectOfType<Quiz>();
        endScreen = FindObjectOfType<EndScreen>();
    }

    void Start()
    {
        quiz.gameObject.SetActive(true);
        endScreen.gameObject.SetActive(false);
    }

    void Update()
    {
        if(quiz.isComplete)
        {
            quiz.gameObject.SetActive(false);
            endScreen.gameObject.SetActive(true);
            endScreen.ShowFinalScore();
        }
    }

    public void OnReplayLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

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

i added my own Quit button i have tried removing this just in case it cause an issue but still getting it so nothing to do with that

I’d guess quiz or endScreen is null. Perhaps they’re missing in the scene or they’re missing their appropriate scripts.

EndScreen.cs is added to Win Canvas and Quiz.cs is set to Quiz Canvas

Hi,

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.

nothings empty and the lines i already said here nothing seems to be wrong

Could you please paste the line of code here to which the error message is referring? Since the forum does not show any line numbers, line 27 is a bit difficult to find.

1 Like

Make those public or SerializeField and see if Unity really is picking them up. If they’re null in the inspector, that’s where you should look.

i made them SerializeFields and it shows as none but im not sure what i need to do to fix it

Since doing this i noticed that once i press play what ever i put in there will be removed from the field

See short vid here Screen capture - 1c3fff8b1253a1fa27414a38a972eff1 - Gyazo

Why are you executing quiz = FindObjectOfType<Quiz>(); in Awake if you manually assigned a Quiz object? Could it be that the Canvas game object or the Quiz object are getting destroyed after you clicked the Play button? Or did you assign a prefab to the Quiz field in the Inspector?

There must be a Quiz object in your scene. Otherwise, quiz = FindObjectOfType<Quiz>(); will return null (= no object reference).

1 Like

I done it so i can see if anything was set just as the previous user suggested.

After going outside for a bit and coming back. I decided to just do it manually.
i put SerializeFields for Quiz and EndScreen and set them in the editor and removed both lines of FindObjectOfType. This has worked. In the editor i have set EndScreen to be the Win Canvas and Quiz as the Quiz Canvas.

here the code for my game manager now for reference if anyone else ends up having issue you can see what i done to resolve it.

{

    [SerializeField] Quiz quiz;
    [SerializeField] EndScreen endScreen;

    void Start()
    {
        quiz.gameObject.SetActive(true);
        endScreen.gameObject.SetActive(false);
    }

    void Update()
    {
        if(quiz.isComplete)
        {
            quiz.gameObject.SetActive(false);
            endScreen.gameObject.SetActive(true);
            endScreen.ShowFinalScore();
        }
    }

    public void OnReplayLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

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

1 Like

Good job on solving the problem! :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms