Creating a Spawn Manager

David Little
3 min readJul 14, 2021

The ability to create game objects for the player to to destroy is a key component of this game. At this point in development we have the player object, the enemy and lasers. So, we now need to build a function that will create the enemy ships. Because, no enemies == no goal/target/or fun. We start by creating a script called SpawnManager.

We also need two empty objects in our Hierarchy. One is called EnemyContainer and it is nested inside of the Spawn_Manager.

Next we need our variables. They are for the GameObjects; EnemyPrefab, and EnemyContainer so that we can call them up or reference them when needed.

In the start function we want to call out the coroutine we’ll be using further on in the script.

For the SpawnRoutine itself you would think, based on past examples, that we would call it using void SpawnRoutine(). However, we will instead use IEnumerator. The reason for this is that it will allow us to interrupt the function with a wait. This is done to keep loops from running forever or to orchestrate a series of events with timing.

Next, when enemies do spawn, we do not want them always being created in the same spot. It would be far too easy for the player to just find a good parking spot and shoot everything that shows up on that same spot. Sure their score would go up quick but the game would be boring to play.

We do this with a Vector3 and something new, a position to spawn, or a new local variable called posToSpawn, in this case. We also need to Instantiate the enemy objects. But, we need to make sure that our random position is generated first. So the order is to create variable, generate a random value for it, Instantiate our enemy at the position, and do so in the proper _enemyContainer within the SpawnManager game object. Lets look at that first one. Create the variable and generate a value for it.

Next, Instantiate the enemy at the randomly generated position.

…and do it all in the _enemyContainer.

Together we get this in our SpawnManager.

Now we have a spawn manager.

In the next article we’ll talk about the coroutine that needs to be added and how to know when to stop spawning enemies.

--

--