Search Unity

TextMesh Pro Change text in script NullReferenceException

Discussion in 'UGUI & TextMesh Pro' started by RalucaNae, Jul 10, 2019.

  1. RalucaNae

    RalucaNae

    Joined:
    Jun 28, 2019
    Posts:
    2
    I'm making a VR scene and trying to display the time left in game in a TextMesh Pro. I'm updating the time in a script, with a public TextMeshPro variable so I can pass the object there, but it throws a NullReferenceException.

    Hirarchy : (parent) emptyObject with script attached -> (child) Text - TextMeshPro

    Script code:
    public TextMeshPro timeText;
    ...
    void Update()
    {
    if(timeLeft >= 0){
    timeLeft -= Time.deltaTime;
    timeText.text = "Time: " + ((int)timeLeft).ToString (); }

    The weird thing is that the text gets updated on the screen, but it still throws a NullReferenceException on the last line of the Update() function and says that timeText is null. Any idea what's happening here?