Search code examples
unity-game-engineunity3d-gui

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.


Solution

  •     var canvas = UIManager.uiManager.NewCanvasMenuCityPrefabs[Number].transform;
        for (int i = 0; i < canvas.childCount; i++)
        {
    
           var textComponents =  canvas.GetChild(i).GetComponents<Text>();
        }