Spawning Objects in an Organized Manner

Keeping our game organized

Marc Frappier
4 min readApr 13, 2021

When our game has enemies or special powers, etc. it would be nice if we could get the script to generate them automatically within our parameters.

For this, we can create a spawn manager.

Lets start of by creating an empty object in the hierarchy.

Create empty GameObject

Now we must create the SpawnManager script. So we right-click on the Script folder, select Create, C# Script.

Creating new SpawnManager C# Script

Lets open the SpawnManager script. Before the Start function, lets create a Serialized variable of type GameObject.

GameObject variable

This variable is so that we can hold our enemy object within the SpawnManager object.

To do this, now that we have the variable created, back in Unity, lets drag our SpawnManager script over the SpawnManager GameObject and add it as a component.

Adding script as component

Notice that in the SpawnManager (Script) in the Inspector, we can see the variable we created in the script, Enemy Prefab. Next to it it says None (Game Object). This is where we need to insert our enemy prefab object from the Prefab folder.

Now the enemy object is tied into the SpawnManager script.

Back in our SpawnManager script, lets create a coroutine in order to spawn our enemies.

We will use the IEnumerator and inside we will use an infinite while loop.

Inside the infinite while loop, we are creating a Vector3 variable posToSpawn, in order to generate a random X position for the enemy objects to spawn. We then Instantiate the actual enemy object calling upon the GameObject variable we created earlier. Finally, in order for the coroutine to work, the yield keyword where we will set a pause timer of 2 seconds between enemy spawns.

In order for this coroutine function to work, we need to start it. This is done inside the Start function.

StartCoroutine()
Enemy objects being spawned

We now have our spawning system running. But, if we take a closer look at the Hierarchy window, we can see it filling up with cloned objects. This clutter doesn’t look good and makes our Hierarchy look unorganized.

Spawned enemy objects in Hierarchy

Oganizing the Hierarchy

Sometimes we would just destroy an object when it moves off scene, but there are time we need to keep the objects for later checking. If this is the case, we need to make sure these objects don’t clutter up our hierarchy window.

In order to accomplish this, we can create a folder specifically for these objects.

So, lets start off by creating an Empty object in the Hierarchy, name it EnemyContainer, and place it inside the SpawnManager Object as a slave.

Creating new Empty object inside another object

Now, lets go to our SpawnManger script. Here we need to create a serialized private GameObject so we can connect the new container to the script.

Back in Unity, if we select the SpawnManager container in the Hierarchy, we notice that the new GameObject we created in the script appears in the Inspector.

If you notice, it says None (Game Object). We need to drag the new Enemy Container object into this slot.

Connecting the two containers

Now we need to let our SpawnManager know that it should spawn the enemy objects from our Instantiate into this container.

Lets go back to our SpawnManager script, down to out IEnumerator coroutine. this is where we are spawning our enemy GameObjects. In order to let it know where to place the instantiated objects, we must first assign these objects to a GameObject variable. Now that we have the enemy object assigned to a variable we can tell it where to spawn the objects.

Directing the new objects to spawn in a new container

Now, if we run the this in Unity, we can see that the new enemy objects are spawned inside the new EnemyContainer.

This way we can keep our game organized and clean.

--

--

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.