Western Shooter Devblog #7 | Using Coroutines to Build a Spawn System in Unity

Ethan Bristowe
2 min readMay 5, 2021

--

We’ve touched on Coroutines before in previous devblogs for our bullet destroy timer, but we haven’t even scratched the surface on the usability of Coroutines! Coroutines will be absolutely VITAL in making our spawn system so let’s begin using them!

Customizable Enemy Spawn Settings

We need to create a foundation with scalability in mind so we can apply this system to all our enemies in the future. So let’s start by making a bunch of serialized variables that we want to be able to control for our enemies.

So here we have a toggle if we want to spawn that type of enemy, a container to place our enemy prefab, a variable to set our Spawn Cooldown for that enemy, and lastly we can set how many of that enemy we want spawned in this level.

These variables will give us our Customizable Spawn Settings that we can control in the inspector! But let’s give these control some purpose!

First control setting done! Our Spawn Enemy Coroutine will only be called if we turn it on in the inspector!

And there we have the rest finished! I built a Coroutine with a While loop which means that it will run our Coroutine over and over again until it’s parameter becomes false. In this case I have our _amountEnemyAHasSpawned and our _enemyASpawnAount being compared each loop which will turn false once the amount of enemies that have spawned has reached the number of enemies I set to spawn in the inspector.

We have our Spawn Cooldown which represents how much time inbetween spawns we want, which we can of course set in the inspector.

And next I made a simple switch statement with the pure purpose of choosing between the two spawn points we created and then Instantiating our enemy afterwards!

We’re getting close to done with this Spawn System! We’ll get into how we will further Randomize our Spawn Points in the next devblog so stay tuned for that!

--

--

Ethan Bristowe
Ethan Bristowe

Written by Ethan Bristowe

Passionate self taught game developer excited to learn and share everything I can about game development!

No responses yet