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");

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";

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>();