Spawn Managers and Object Pooling

A Spawn Manager controls when and how the targets are spawned in.

Manager classes in Unity are special gameobjects with specific script structures attached to them. In this case, the Spawn Manager is a Singleton, one of the most common structures used.

This code ensures a duplicate can never exist for the Spawn Manager.

A Singleton is a script that can only exist once, and holds important data for other scripts to access as needed. This is commonly used for Game Managers, UI Managers, and Sound Managers as well.

In this case, I am storing the spawn location, the end goal location, and the safe zones in this manager class so the targets immediately have access to the data they need as they spawn in.

Each target needs this information. It is less resource intensive to hold all the data on the manager (which is always active) then to manually find this data for each target spawned.

As for how the spawner actually functions, I set the _target value to be a prefab (the white cube for now) and dragged it into the appropriate slot in the Inspector.

The Spawn and End locations are also linked the same way.

Now that the relevant information is known, its as simple as instantiating the prefab at the spawn location.

The final line (48) places the spawned targets into an empty gameobject for hierarchy cleanliness.

Keeping the targets inside this gameobject stops the hierarchy from becoming too cluttered.

This concept is known as Object Pooling, as is used not only for enemy spawning, but projectiles as well. While this example will not be using projectile objects (we will be using Raycasts), it is a good habit to get into to keep your designer(s) from wanting to throttle you!

To loop the spawner to keep pumping out targets, I used a while statement to make a new spawn occur every 2 seconds.

_isgameActive is a Boolean that defaults to true.

The next stage will be to introduce a Finite State Machine (FSM) system for the targets to run, hide, and be destroyed correctly. I will also be adding some random modifiers to targets, changing their speed and size to give them some much needed variety!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jack Leavey

I am a software engineer with years of experience branching into game development, specifically in Unity. Follow along for guides on creating game mechanics!