- Home /
Extract GUIText from GameObject
Hierarchy of my game objects (excerpt):
player{1|2}Text
are GUIText objects.
I would like to access these GUIText objects and modify their texts.
My code:
print (GameObject.Find ("player1Text"));
print (GameObject.Find ("player1Text").GetComponent<GUIText>());
print (GameObject.Find ("player1Text").GetComponentInChildren<GUIText>());
print (transform.Find ("player1Text").GetComponent<GUIText> ());
Output:
It seems that the first call actually finds the game object, however, a GUIText component cannot be found.
I greatly appreciate any hints and/or ideas.
Are you absolutely sure that those objects have GUIText components attached to them? Also, are the GUIText components active?
oatsbarley: Thanks for your suggestion! I checked again and I found out that I was using text meshes (3D text objects) ins$$anonymous$$d of GUIText.
Answer by gfallasc · Jan 24, 2014 at 04:27 PM
Hi, I think there are different ways to get a component. In case you want to find it directly using GameObject class you should give the absolute path:
print (GameObject.Find("ScoreSystem/player1Text").GetComponent<GUIText>());
or you if your script is attached to the parent object you could find them in the hierarchy using the transform.FindChild("childName");
then get the gameObject and get its component or directly use the gameObject.GetComponentsInChildren();
I hope it helps
Thanks for your answer. As I've written above in a comment, I confused GUIText objects with text meshes (from 3d text objects). I accept your answer nonetheless to give you credit (I can't upvote yet)!
Your answer
