- Home /
GetComponent().text
StarsToUnlock.GetComponent<Text>().text = "TEST";
I've got this line setup, of which I'm pretty sure it should work... StarsToUnlock is referenced as a GameObject, and is set by using GameObject.Find. No problems finding it, but when the script tries to edit the text on the Text component it just gives a nullreference exception. The object it searches for is a standard Unity 5 Text object (UI).
did you confirm that there's a Text component on that game object?
"The object it searches for is a standard Unity 5 Text object (UI)." Yes ;)
what's the exact error message? the NRE is typically generated if there's no Text component, but if you're 100% that it exists, then ...
Answer by Crixu · Nov 03, 2015 at 06:14 PM
Screenshot Here's a screenshot of the component, and I made sure it has a unique name, so it's not finding another gameobject and trying there.
NullReferenceException: Object reference not set to an instance of an object
Password.Start () (at Assets/Portal Minigame/_Scripts/Password.cs:91)
This is the error. The code it's referencing to is the line stated in the question.
It's all executed in the Start() function, but I fixed it by putting the code in a if(StarsToUnlock != null) statement....
Anyhow, thanks for helping and eventually leading me to a solution!
Your answer