My 120-day journey to being a better Unity Developer — Day 14

Spawning Objects in Unity without the Clutter

Jean-Noel Seneque
3 min readMay 11, 2021
Enemies spawning at the bottom of the Hierarchy View

Objective: Organise the instantiate enemies into a container to keep the Hierarchy view nice and tidy.

In the last article, Coroutines with Unity, I show how to implement a Spawn Manager that uses a coroutine method to spawn enemies at a set interval. By doing this, I caused a mess where the Enemy is being created and appearing at the bottom of the Hierarchy and it won’t be long before this will clutter the view.

Let’s begin. In the Hierarchy view, right-click the SpawnManager and Create Empty. Rename to EnemyContainer.

Create an EnemyContainer

Now that we have our container, we now need to let the SpawnManager know about it. We do this by adding a reference to it in the code. Save the script.

_enemyContainer stores the reference to the container

Head back into Unity and click on the SpawnManager. You should see a new spot in the SpawnManager component to drag on the Enemy Container.

Drag Enemy Container into the Spawn Manager script

In the code’s current form, when the enemy is instantiated, we have no control over this object once it has been create and let loose in the wild. We need to store a reference to this game object in a variable so we can make changes to it.

In the code snippet above, we store a reference to the enemy instantiated. Then we set the enemy’s parent to be the enemy container.

Now when we play the game, as each enemy appears, they are neatly organised in the Enemy Container to bring order in our world.

FINAL

Enemies now appear in the Enemy Container
Spawn Manager script

If you enjoyed reading this article give me a Clap, also if you want to see more like this, “Follow” me, so you may be notified of future releases. You may also send me a message if you need any further help.

--

--

Jean-Noel Seneque
Jean-Noel Seneque

Written by Jean-Noel Seneque

A Data & Analytics Consultant who is expanding into developing experiences in XR, Enterprise and Gaming space using Unity www.jeannoelseneque.com

No responses yet