Search code examples
c#unity-game-enginegameobject

How to control instantiated prefabs at runtime as children


I have a game in Unity where I instantiate GameObjects every few seconds. Then in the Hierarchy, the prefabs show up in a list of them being instantiated e.g.:

EnemyBlue(Clone)
EnemyRed(Clone)
EnemyGreen(Clone)
EnemyBlue(Clone)

Which clogs up my hierarchy. My question is, is it possible to instantiate GameObjects as a child of an empty GameObject, for example:

Enemies // An Empty GameObject
    EnemyBlue(Clone)
    EnemyRed(Clone)
    EnemyGreen(Clone)
    EnemyBlue(Clone)

Solution

  • You can set the parent of a transform like ILiveForVR mentioned, but you can also set the parent for something that already exists:

    gameObject.transform.parent = GameObject.Find("Name of game object").transform;