Getting a NullReferenceException

So I have the code the exact same as it is in the lesson and I keep getting this NullReferenceException. I Serialized the label field to check and the code just isn’t putting anything in there which I’m guessing is the problem. Has TMP changed something since the lesson came out or whats exactly going on here?

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

[ExecuteAlways]
public class CoordinateLabeler : MonoBehaviour
{

    [SerializeField] TextMeshPro label;

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

    void Update()
    {
        if(!Application.isPlaying){
            DisplayCoordinates();
        }
    }

    void DisplayCoordinates(){
        label.text = "--,--";
    }
}
2 Likes

For anyone who gets this error in future, make sure your script is on the TMP Text object and not on the Parent “Tile” object. Doesn’t work so good if ya do it wrong

1 Like

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

Privacy & Terms