Search Unity

Get all children components

Discussion in 'Scripting' started by getmyisland_dev, Jul 8, 2021.

  1. getmyisland_dev

    getmyisland_dev

    Joined:
    Dec 22, 2020
    Posts:
    100
    I want to get all my childrens components to change the button text to whatever I want, but what it does is it only changes the text of my first children.

    Here is the line of code I'm having trouble with:
    Code (CSharp):
    1. GetComponentInChildren<Text>().text = "Hello World";
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Code (csharp):
    1. var allTexts = GetComponentsInChildren<Text>();
    2. foreach (var thisText in allTexts) {
    3. thisText.text = "Hello world";
    4. }