Spawning Objects In Unity Without the Clutter

Austin Hall
3 min readSep 23, 2022

--

The object here is to keep our Hierarchy tab as clean and organized as possible. With our current setup, we will see all of the enemy objects spawning in the Hierarchy tab. We want to make the enemy a child object of the spawn manager, so that the enemies will spawn inside the Spawn Manager object.

Hierarchy for Enemy Container

For this, we’ll need to create an empty object and name it Enemy Container. To make it a child object of the Spawn Manager, we need to right click on the Spawn Manager object and select create empty object.

Right Click on Spawn Manager and Create Empty

We’ll name the object Enemy Container. For us to see this object in the Spawn Manager, we need to create the GameObject in our script.

Create Private Variable with SerializeField

Now, we need save and go back to the editor. Select the Spawn Manager Object in the Hierarchy tab, and you should see the Enemy Container variable, listed with no reference, in the Inspector tab. Click and Hold on the Enemy Container object in the Hierarchy tab and drag it to the Inspector tab and place it in the reference box for the Enemy Container variable.

Then we will go into our coroutine for spawning enemies, in our Spawn Manager script, and create a new GameObject variable.

Spawn Enemy Coroutine in Spawn Manager

For the enemies to spawn in the enemy container we have to do a couple of things. First, we need to store the instantiation of our enemies in our GameObject variable, ‘newEnemy’. Now when we Instantiate the enemy game object, we can access it through ‘newEnemy’. Second, we want to get the parent object of the game object we just instantiated. we do that with newEnemy.transform.parent. Now, all we need to do is assign it to the parent of our Enemy Container GameObject.

newEnemy.transform.parent = _enemyContainer.transform; We can do this because the parent object is also of type transform.

The end result is a very clean look on our Hierarchy tab and the enemies spawning inside the container.

Enemy Container Spawning the Enemies as Child Objects

--

--

No responses yet