NullReferenceException Error when using Text Mesh Pro

I’ve ran into an issue where I keep getting the following error: “NullReferenceException: Object reference not set to an instance of an objectScoreDisplay.Update () (at assets/Scripts/ScoreDisplay.cs:23)”

I’m currently working on the “Add and Display Score” section of the Lecture and the issue appears to be related somehow to Text Mesh Pro, as I have deleted the text and recreated as just a regular text object and I have not issue the code works at is should, however using Text Mesh Pro I keep getting the same error.

Any help would be greatly appreciated!

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

public class ScoreDisplay : MonoBehaviour
{
    Text scoreText;
    GameSession gameSession;
    
    // Start is called before the first frame update
    void Start()
    {
        scoreText = GetComponent<Text>();
        gameSession = FindObjectOfType<GameSession>();

    }

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

Hi Donald,

If we look at your error message it points to line 23 in the Update method of ScoreDisplay.cs. Rather conveniently there is only one line of code within the Update method so that’s rather useful in narrowing down the problem.

A NullReferenceException error is caused when you try to access a member of an object that is Null, e.g. there is no reference. A member being a property or method of that specific instance of a class.

Looking at the line of code within the Update method;

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

…there are two potential places where this could be happening;

  • you are accessing the text property of scoreText
  • you are accessing the GetScore method of gameSession

One of those two objects is Null.

In the case of scoreText this often happens if you have forgotten to drag across the Text UI GameObject into an exposed field, but looking at your code we can see that this script is looking for the Text component, meaning that this script should be attached to a GameObject with the Text UI component already, and probably is, but worth checking.

I note that you mentioned TextMeshPro, these GameObjects do use a different text of component, so you may find that you do not have a Text component on the GameObject but a TextMeshProUGUI for example. If this is the case, you’ll need to update your GetComponent to use the correct type.

In the case of gameSession, it is possible you haven’t added this to the scene, e.g. a GameObject with the GameSession.cs script attached.

Could you check the above and let me know what you find.

Hope this helps :slight_smile:

1 Like

That solved it! Thank you so much!

The issue was that I need to use the TextMeshProUGUI component instead of the Text.

Thank you again. :slight_smile:

1 Like

You’re very welcome :slight_smile:

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

Privacy & Terms