Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

[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:
    36,971
    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.