Fix for error :NullReferenceException

I wrote the code exactly as Rick said at the lesson but I got stuck with a nagging error " NullReferenceException: Object reference not set to an instance of an object"
So instead of using
scoreText = GetComponent<TMP_Text>();
I used
scoreText = GetComponentInChildren<TMP_Text>();

and that simply fixed the error since the score bored is a child of Canvas.

your object does not have the Text - TextMeshPro component, so when you do GetComponent<TMP_Text>() it will return null (that mean does not exist)

Privacy & Terms