Spawn Managers and Object Pooling

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.

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.

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.

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.

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.

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

Jack Leavey

34 Followers

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