# Change Text - Inside A button, inside a Canvas, inside a GameObject: Access all the texts inside of hierchy?

//....
Gameobject tile;
int count = 1f;
float posX = 0;
float posY = 0;
....//

tile = (GameObject)Instantiate(blueTile, transform);

for loop {
...
...
tile.GetComponent().GetComponentInChildren().text = count.ToString();
tile.transform.position = new Vector2((float)posX, (float)posY);
count++;
}



As you can see by the image. My line of thinking goes like this; I used to create Flash/AS3 Applications and Games. I have used a number of API's and they usually have a lot in common. Unity, however, is very different and I may have not grasped the key concepts.

I want to access my PreFab, named in Assets/Resources (the root path)
"block_blue"

All I wan to do is set up a row and columns of a box with a number inside of it.

Before you go and ask, why the complicated mix of Objects? Well, Adobe had MovieClip and use of layers and also use of a timeline. Unity doesn't seem to have this in setting up a Scene (the Stage in Flash).

So, I set up a block that looks like this (right of main image 66): It's a Canvas, to hold a Button, the button has a text. To move the Canvas, I put the Canvas inside a GameObject. This, to me, is like a MovieClip in Flash API.

**GameObject > ButtonLight, Button > TextBlack, Text**

I need to access TextBlack and also Text. Pretty easy right? I've been at this for 4 days, many many hours.

EDIT: It seems that it is working, somewhat.
Looking at the reference photo and the Hierarchy reference, it looks like the blackText behind the blueText is changing as expected. But I cannot access the top BlueText. The Blue Text is also a child of type Text, but it's a second (layer?) copy of the text inside of the Button. So, it's getting the first Child Text, but how to access the second Text?

• Unity Doc, not good. If you are a Unity Expert, maybe? But for reference as a person learning Unity, not helpful at all. It's like an Engineering schematic, not a reference manual. No examples. Here's a pointer, what a pointer looks like. It's why I even kept my old C books, for the examples in the text. But now API's change so often, it's not even worth buying texts. "this decision on your coding choice is deprecated" - which means "the masters of this API changed it dude, so get lost with that". It's a contributing reason I decided to stop pursuing a profession in/and use my CS degree. Dec 21, 2020 at 20:40

GetComponentInChildren traverses the whole object tree below an object using a depth-first search until it finds the first component of the requested type and returns it. I would not advise to use that method unless you are in a situation where you are certain that there is only one child with that component and there will never be a reason to add a second one.

If you want to get all the text components in all the descendants of an object, use GetComponentsInChildren. Note the extra s behind Component. It doesn't just return the first Component it finds. It traverses the whole tree collecting all the components of that type and then returns them as an array of 0 or more Components. So when you want to change the color of all the texts of a given object, do

foreach(UI.Text text in theGameObject.GetComponentsInChildren<UI.Text>()) {
text.color = Color.blue;
}


Caveat: When you have very large object hierarchies, then this method can become slow. With object hierarchies of 5, I don't think you are at that point yet. But it might become a problem with 100s of nested objects.

So when you want to change the color of a large number of text nodes in a deeply nested hierarchy very frequently, then I would look if it's possible to assign the same Material to all the texts and control the color by changing that shared material.

By the way: You can find a lot more useful methods for navigating the scene graph in the documentation of the GameObject class.

• This seems logical; haven't grasped the concepts yet. But familiar with overhead,cost. Getting started with Unity. Flash/AS3 went down it was tough. Android-API difficult for mundane tasks. I only found Unity, but took took time to decide to plunge into the API. I did a lot of refactoring recently on this project. Layers (easy, my world) but then Z messes up (auto-assign). It took me days to figure out the culprit. Like Z trumps layers? It's buggy in that design. And then Prefab, original, partial Fab? Huh? Materials and Shaders, uh-oh sounds like 3D. Took months to get familiar w/ Blender. Dec 21, 2020 at 20:32
• @ejbytes Please ask any new questions you have which are not directly related to this answer as new questions. Dec 22, 2020 at 1:51

As you can see by the images, I had to change the "Hierarchy". In the attempt to utilize this as we do in Adobe Flash (AS3) in using-layers, this is how I accomplished this. To give it a "MovieClip" Feel from the Adobe Flash API approach.

I replaced ButtonLight in the hierarchy, with Image.

I removed TextBlack from Button and Added a Shadow component to the Text inside the Button Component. The new hierarchy is in the image.

Here are the 3 syntax I used, with this Hierarchy, to accomplish the goal:

Hierarchy

• GameObject
• Image - > Color
• Button - > Image - > Color

Text - > Color & Shadow Component instead of a second Text

Button Color

• public void changeButtonColor(GameObject gameObject, Color color)
• gameObject.GetComponentInChildren().GetComponent().color = color;

Font Color

• public void changeFontColor(GameObject gameObject, Color color)
• gameObject.GetComponentInChildren().color = color;

Image Color

• public void changeTileColor(GameObject gameObject, Color color)
• gameObject.GetComponentInChildren().color = color;

I was able to create 3 functions/methods to change the attributes within the Hierarchy Layers of the GameObject Prefab, "block". Where, the "block" is a GameObject with the given hierarchy above. A layers approach.