Search Unity

TextMesh Pro TextMeshProUGUI unassigns when in play mode

Discussion in 'Unity UI (uGUI) & TextMesh Pro' started by thigoap, Aug 24, 2019.

  1. thigoap


    Apr 18, 2016
    I'm new here. I've looked a lot about this problem I'm facing, but no topic has shown the same issue.

    I've followed this tutorial ( to make a Text updates every time my scripted score changes and it worked fine.

    But i'd like to use TextMeshPro instead of the simple Text. I've already made all the necessary changes in my code, such as:
    • using TMPro
    • public TextMeshProUGUI scoreText; (instead of public Text scoreText)
    • scoreText = gameObject.GetComponent(); (inside void Start())
    • scoreText.text = "Score: " + score.ToString(); (inside my function)
    When in Play Mode I receive the "NullReferenceException: Object reference not set to an instance of an object" message. I discovered the reason. When I enter Play Mode, I see in the Inspector that the TextMeshProUGUI that was assigned to the script is automatically unassigned.

    I've made a stand alone script and added to a TextMeshProUGUI to check if my code was working with TextMeshPro, and it was. The thing is: I don't want one separated script only to update the TextMeshPro. I need, as the tutorial above, the TextMeshPro being a public variable inside a big script with all other functions.

    If, after I enter Play Mode, I assign the TextMeshProUGUI manually by dragging it to the Inspector, the Score works! Why Texts, preFabs and everything stay assigned to my gameObject when I enter Play Mode, but the TextMeshProUGUI doesn't?

    I think the code is ok, because it works if I assign the TextMeshProUGUI manually. But, anyways... some parts of it.

    using TMPro;

    public class SnakeController : MonoBehaviour
    public TextMeshProUGUI scoreText;
    private int score;

    void Start()
    scoreText = gameObject.GetComponent<TextMeshProUGUI>();
    score = 0;

    public void setScoreText()
    scoreText.text = "Score: " + score.ToString();
    The big issue is the TextMeshProUGUI be unassigned in the moment I enter Play Mode.

    Before enter Play Mode.

    Immediatelly after enter Play Mode
  2. Stephan_B


    Unity Technologies

    Feb 26, 2017
    Is your script with this "public TextMeshProUGUI scoreText;" on the same object that contains the TextMeshProUGUI component or is this script attached to some other object in your scene?

    If your script is attached to some other game object then in Start, the gameObject.GetComponent<TextMeshProUGUI>() will not find the text component and as such end up null. So if this script is attached to some other game object, remove the GetComponent from Start since you are already assigning the text component via the public field in the inspector.
    thigoap likes this.
  3. thigoap


    Apr 18, 2016
    Fantastic! Thanks a lot!
    You were correct in your assumption. The TextMeshProUGUI usually (or always?) must be inside a Canvas. So, it was in a different object.
    I just deleted the line where scoreText was being assigned via script inside void Start(), and everything is working as expected now.