Search code examples
c#unity-game-enginegameobject

How to get array of child objects contained in a GameObject


I have a GameObject in Unity that is supposed to serve as a container for some definitions.

object explorer

I'd like to access that object and retrieve the Def class instances (every object there is instance of Def general class).

So, if I have a GameObject instance, how can I retrieve all objects that are instances of specific class?


Solution

  • You can use the GameObject.GetComponents<Def>(); to retrieve all components of Def type in the GameObject.
    More info in the Unity Docs http://docs.unity3d.com/ScriptReference/GameObject.GetComponents.html