Unity: Creating a spawn routine

Objective: We need to create a new script to create a spawn manager that handles spawning the enemies every 5 seconds.

First, we need to make a reference for the spawn manager to the enemy so the spawn manager knows what it needs to spawn. Then, we need to work with an IEnumerator which will allow us to create a routine and gives us access to the yield function.

Next, we need to use a while loop which we need to be careful with because this can cause a crash so, don’t run this script until you finished it. For now we can keep the while loop true and then use the instantiate method since we will be spawning the enemy prefab. You will need to create a new spawn position so the new enemy prefabs spawn at the top of the screen. Once that done we will need to use the yield function so that we can tell the spawn manager to wait for 5 seconds so it doesn’t spawn enemies so fast that it crashes.

After that we need to tell the spawn manager to start running this routine at the start of the game so in void start we need to initialize the routine method.

--

--

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
Jake Boere

Jake Boere

7 Followers

I am hard working Game development student learning as much about game development and unity as possible.