Null Reference Exception

I am using Unity 2018.2.14f1

I have verified that I have the Text object created under my Canvas

I have deleted my code and copy and pasted in the code from the course

My score will not update.

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

public class ScoreDisplay : MonoBehaviour
{

    Text scoreText;
    GameSession gameSession;

    // Use this for initialization
    void Start()
    {
        scoreText = GetComponent<Text>();
        gameSession = FindObjectOfType<GameSession>();

        Debug.Log(GetComponent<Text>().name);
    }

    // Update is called once per frame
    void Update()
    {
        scoreText.text = gameSession.GetScore().ToString();
    }
}

Yet when I run this my Text stays set to 0 and I get this message

NullReferenceException: Object reference not set to an instance of an object
ScoreDisplay.Start () (at Assets/Scripts/ScoreDisplay.cs:18)

Then on update

NullReferenceException: Object reference not set to an instance of an object
ScoreDisplay.Update () (at Assets/Scripts/ScoreDisplay.cs:24)

When I try logging the scoreText instead of coming back as an object it comes back as null.

I have deleted and recreated the

UI->Text component several times and it keeps giving me the same error.

My source code for this is view-able here

1 Like

Hi Steven,

From your code, the errors are occurring on these lines;

Debug.Log(GetComponent<Text>().name);

and

scoreText.text = gameSession.GetScore().ToString();

The NullReferenceException error is caused by code trying to access a member of an object for which there isn’t a reference.

In the case of the second error, line 24, it’s possible this could be either scoreText or gameSession, as those are both objects, but being that you getting the error on line 18 also, the Debug statement, I would suggest in both cases it’s likely to relate to there not being a Text component on the same GameObject to which your ScoreDisplay script is attached, as if we follow your code back, in both cases it relies on GetComponent<Text>.

So, which GameObject in your scene has the ScoreDisplay script attached to it? Once you have found that, check to see if there is a Text component on that GameObject also.

One other thing to consider, check that you haven’t added the ScoreDisplay script component to multiple GameObjects.

If you still can’t find the issue, zip up your project files and share them and I will take a quick look for you :slight_smile:

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Hello Rob,

Thank you for the quick response.

I finally figured out my issue.

I attached the script to the wrong game object. I had attached it to my GameSession object instead of the Text Object itself.

I figured it out after looking through the documentation and realizing GetComponent is looking for a component on the object it is currently attached to.

Once I moved the script over to the correct object everything was solved.

1 Like

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

Privacy & Terms