How to get all the children of an instantiated Canvas Element Prefab

1.8k views Asked by At

I have instantiated a Scroll Rect Prefab inside a canvas and now I need to get all the children of that instantiated prefab and also the text components in each one of them.

I failed to get them with the code below. UIManager.uiManager.NewCanvasMenuCityPrefabs[Number] is the reference to the instantiated canvas element prefab.

Transform[] Parent = UIManager.uiManager.NewCanvasMenuCityPrefabs[Number].transform.GetComponentsInChildren< Transform >();

foreach (Transform Child in Parent)
{
     print(Child);
}

But still I couldn't get any of the children of the element.

1

There are 1 answers

1
Marius Junak On BEST ANSWER
    var canvas = UIManager.uiManager.NewCanvasMenuCityPrefabs[Number].transform;
    for (int i = 0; i < canvas.childCount; i++)
    {

       var textComponents =  canvas.GetChild(i).GetComponents<Text>();
    }