Extract GUIText from GameObject

Hierarchy of my game objects (excerpt):

21104-unity-hierarchy.png

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:

21107-unity-screen.png

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.

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