Sreejit Pillai
Sreejit Pillai

Reputation: 125

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

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.

Upvotes: 1

Views: 1976

Answers (1)

Marius Junak
Marius Junak

Reputation: 1213

    var canvas = UIManager.uiManager.NewCanvasMenuCityPrefabs[Number].transform;
    for (int i = 0; i < canvas.childCount; i++)
    {

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

Upvotes: 3

Related Questions