- Home /
Assign GUIText to GameObject via Script
Hello, can anyone write me any simple code explaining how to assign GUIText from my Hierarchy to GameObject via script? Thanks for any response.
Something like:
GUIText myGuiText = GUIText.FindGameObjectWithTag("myGuiText");
Answer by Tomer-Barkan · Nov 15, 2013 at 07:58 PM
FindGameObjectWithTag()
finds a GameObject
, like the method name suggests. It does not find a GUIText. Also note that FindGameObjectWithTag()
is a static method of GameObject
class, not of GUIText
class.
So you'd want to use this:
GameObject myGuiTextGameobj = GameObject.FindGameObjectWithTag("myGuiText");
GUIText myGuiText = myGuiTextGameObj.GetComponent<GUIText>();
myGuiText.text = "Some text";
Thanks for response, but it is still not assigned in the object when I start the game.
Simply, I am looking for any way how to assign GUI Text(from hierarchy) into GUIText variable without Drag&Drop it to slot in the hierarchy, but via Script.
That's exactly my code. If you have a GUIText object called "myGuiText" in the hierarchy, the code above will fetch it, and change the text to "Some text", without needing to assign anything in the inspector.
Answer by panbake · Nov 15, 2013 at 08:14 PM
This will grab the GUIText component off a gameobject in your hierarchy with the name MyGuiText. (in C#)
GUIText guiText = GameObject.FindGameObjectWithTag("MyGuiText").GetComponent<GUIText>();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Gui Text Help 1 Answer
Changing GUI Text colour when health is low 1 Answer
Pairing and GUITexture with a Timer? 1 Answer