- Home /
Referencing to an initiated Text
So I've searched around a bit and I found out, that you can reference to instantiated texts by using
Text myText = GameObject.Find("NameOfMyText").GetComponent<Text>();
Now I'm trying to edit it, like any other text by going for
myText.text = "Any Text";
For some reason this doesn't work for me. My Game tells me, that I haven't referenced the right way, at least I'm getting a NullReferenceException.
What am I doing wrong?
The issue is that GameObject.Find() doesn't find any game object with that name, or it doesn't have a Text component on it, so myText is null. Always check the results of your searches and GetComponent() calls, like this: if (myText != null)
Answer by Pyppo · Feb 08, 2018 at 08:01 PM
Update:
This is getting very weird now. I've kept messing around and found out, that when I use
Text myText = GameObject.Find("NameOfMyText").GetComponent<UnityEngine.UI.Text>();
it works for one of my four texts, but not for the other three. If you have any Idea of whats going on, please tell me. Even a "everything looks right" would still help
thanks c:
Another Update:
I just restarted Unity and now everything works perfectly fine
Your answer
![](https://koobas.hobune.stream/wayback/20220612152232im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Find GUI Button and Assign Text 1 Answer
Multiple Text of canvas with One script ?? 1 Answer
Add multiple ints to a string line. 2 Answers
Problems to change text from a child in a child 1 Answer
Trouble instantiating ui text 2 Answers