2021 LTS - Null Reference Exception - Realm Rush

I was getting the same Null reference exception that others were having.
Tried restarting Unity. Double checked the hierarchy but none of it worked.

For me the solution was to use TMP_Text instead of TextMeshPro. I recall we used TMP_Text earlier in the course.

image

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

[ExecuteAlways]
public class CoordinateLabeler : MonoBehaviour
{
    TMP_Text label;

    private void Awake()
    {
        label = GetComponent<TMP_Text>();
    }

    private void Update()
    {
        // Editor mode only
        if (!Application.isPlaying)
        {
            UpdateLabel();
        }        
    }

    private void UpdateLabel()
    {
        label.text = "x,y";
    }
}

Also to note. It seems newer version of Unity add this Canvas object for TextMeshPro.
I tried removing it, but then the Text didn’t render at all.

Final setup for me in 2021.3.21f1.
I had to change the Snap increments from 0.25 (default) to 10 since the code UnityEditor.EditorSnapSettings.move.x references the snap increments and not my world grid settings.
Also had to set the name on the transform.parent.parent (because of this extra Canvas object).

image

Canvas (for TMPro):

Text (TMP) object:

Increment Snap settings:

Font size:

Final code for this session:

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

[ExecuteAlways]
public class CoordinateLabeler : MonoBehaviour
{
    TMP_Text label;
    Vector2Int coordinates = new Vector2Int();

    private void Awake()
    {
        label = GetComponent<TMP_Text>();
        UpdateCoordinates();
        UpdateLabel();
        UpdateObjectName();
    }

    private void Update()
    {
        // Editor mode only
        if (!Application.isPlaying)
        {
            UpdateCoordinates();
            UpdateLabel();
            UpdateObjectName();
        }        
    }

    private void UpdateLabel()
    {
        UpdateCoordinates();
        label.text = coordinates.x + "," + coordinates.y;
    }

    private void UpdateCoordinates()
    {
        coordinates.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x);
        coordinates.y = Mathf.RoundToInt(transform.parent.position.z / UnityEditor.EditorSnapSettings.move.z);
    }

    private void UpdateObjectName()
    {
        transform.parent.parent.name = coordinates.ToString();
    }
}
1 Like

I was getting a similar error using the same version of Unity. I was able to get mine working without the need for a canvas. I believe the canvas is only necessary for UI elements like the scoreboard in the previous project. From your screenshots it looks like you added a TextMeshPro - Text(UI) component rather than just a plain TextMeshPro - Text component.

When I added a canvas I actually got an error that said “Please remove the canvas object. It is no longer necessary.”

The only changes I made to the code were:

using System; //Added at the top
TMP_Text label; //Changing the label type

@Dex1 You are right. I didn’t know there were two versions of a TextMeshPro Text component.

One from the UI menu:

One from the 3D object menu:

Results:
Screenshot 2023-05-07 at 09.27.41

The one from the UI menu does require a canvas and has slight differences in the inspector than the one from the 3D object menu.

This is really good to know and thanks for spotting that. It bugged me thinking that having a Canvas must be less efficient or just more cumbersome.

Replace Awake to OnEnable

1 Like

The error you are encountering, “NullReferenceException: Object reference not set to an instance of an object,” typically occurs when you are trying to access a member of an object that is currently null. In your case, the error is happening in the DisplayCoordinates method at line 31.
Looking at your code, the issue is likely with the label variable not being properly initialized in the Awake method. When GetComponent() is called, it might not be finding the TextMeshPro component on the GameObject, resulting in label being null.
To fix this issue, you should check if the label variable is null before trying to access it in the DisplayCoordinates method. You can modify the DisplayCoordinates method as follows:

void DisplayCoordinates()
{
    if (label == null)
    {
        Debug.LogWarning("TextMeshPro component not found.");
        return;
    }

    coordinates.x = Mathf.RoundToInt(transform.parent.position.x / UnityEditor.EditorSnapSettings.move.x);
    coordinates.y = Mathf.RoundToInt(transform.parent.position.z / UnityEditor.EditorSnapSettings.move.z);
    
    label.text = coordinates.x + "," + coordinates.y;
}

By adding the null check for label, you can prevent the NullReferenceException from occurring when the TextMeshPro component is not found on the GameObject.

It seems like the TextMeshPro component is not initialized on Awake. OnEnable solves the problem

This is so old but I don’t see an answer

There are two versions of TextMeshPro text fields; one for the UI that, as you noted, requires the canvas, and one that is a game object and does not require a canvas. They exist as two different types and this is where the error comes from. You have defined the game object type in your code (TMP_Text) but you have the UI type (TextMeshProUGUI) in your hierarchy. Unity does the GetComponent<TMP_Text>() and cannot find any because there aren’t any. There is only TextMeshProUGUI. So, it returns a null.

You have to be sure you use the correct versions in the correct places, and then reference the correct versions. If you use it in the UI, you need to reference TextMeshProUGUI and if you use the game object version, you have to reference TMP_Text

Privacy & Terms