How to get a Text child, when there are many Text childs

hello,

i instantiate a prefab (monster-card) to UI.
this monster-card-prefab has many text fields, like name, health, attack, description, …

now i will change the description of this card, but how acces exactly a particular Text element.

but with this i get only the first text element of this card and it changes the name :

newText = newCard.GetComponentInChildren<Text>();
newText.text = "new description"

you need GetComponentsInChildren not GetComponentInChildren :

    Text [] newText ;
	void Start () {
		newText = GetComponentsInChildren<Text> ();
		newText [0].text = "name";
		newText [1].text = "health";
		newText [2].text = "attack";
        newText [4].text = "description";
	}

easiest way is to have the script have multiple public Text variables

public Text Name;
public Text Health;
public Text Attack;
public Text Description;

then you set them inside inspector in unity. now in script you can simply set Description.text. this is useful as it makes your code transparent and easier to read by other programmers

Otherwise lets say that the game object is created dynamically during runtime and you can’t have those inspector values preset. then you can instead use GetComponentsInChildren(). thats with an “s”. which will return an array of all Text components in the children, ordered in the same order as they were placed in the heirarchy.

sorry, placeholder

This might be the easiest to change color of children of a gameobject, you can modify the code to suit your requirement

    public void ChangeColor()
    {   
        foreach (Text text in gameobject.GetComponents.InChildren<Text> ())
        {
            text.color = new Color(1f, 0, 0, 1f);
        }
    }