Search Unity

[SOLVED] A way of grabbing all childs of a gameobject and put them into an array

Discussion in 'Scripting' started by Etaodev, Dec 24, 2019.

  1. Etaodev

    Etaodev

    Joined:
    Dec 8, 2019
    Posts:
    13
    Hello,
    I was wondering if there was a way of declaring a game object as a public variable, and when that variable is assigned the code takes all children of that game object and puts them into an array.

    Or, is there a method where you can instantiate a prefab object and put them into the array upon instantiating them?

    If my question is not clear I can clarify.

    Please help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    If you have a GameObject called
    foo
    , you can get all Transforms in it and all of its children with this:

    Code (csharp):
    1. var AllTransforms = foo.GetComponentsInChildren<Transform>();
     
  3. Etaodev

    Etaodev

    Joined:
    Dec 8, 2019
    Posts:
    13
    Thank you!
     
    Kurt-Dekker likes this.