Nerd For Tech
Published in

Nerd For Tech

Coroutines With Unity!

Creating a repeating spawn system, and keeping our project tidy.

Coroutines in Unity, allow us to run a function that can suspend it’s execution. In simpler terms, we can stop, start, and wait.

A coroutine method in Unity requires an IEnumerator, which needs to contain a yield return type. The most common yield return type is WaitForSeconds. This allows us to set an amount of time in seconds before continuing the function as opposed to the update method which would run our function every frame. A simple coroutine can be great for running sequenced events, instantiating or changing certain game objects across a timeline.

Another common use of coroutines is to create a repeated instance of a function. To do this we need to use a while loop. We can set a function to repeat itself inside of a loop while something is true or false. These loops can be dangerous as they will infinitely repeat if used in the wrong context. Using them in a coroutine makes for a much safer time as it allows us to yield return a time before repeating itself.

We can stop and start our coroutines with StopCoroutine and StartCoroutine respectively, followed by the name of our coroutine and () to call the function in brackets.

We can also manipulate our while loop by creating a bool that can be switched between true or false elsewhere in code.

The above image may look intimidating at first. It’s running a simple spawn manager. Let’s run through it.

First I’m storing a game object type variable to store our enemy prefab.

Next I have another variable storing an empty game object to house all our spawned objects. (More on that later)

Followed by a bool variable _stopSpawn, that we will change in a method called by our player script when we run out of live’s and it’s game over!

In our start method, we are simply calling our coroutine to start.

Our SpawnRoutine coroutine consists of a while loop that checks against our bool we created. This allows the loop to run continuously while the bool is false. Within this I have a similar two lines of code to my enemy wrap in a previous article. I’m using this to set a random spawn position along the x axis.

Next I’m instantiating my prefab, at the location I set above, and following the rotation of my blank spawn manager game object the script is attached to. I’m also assigning each instantiated enemy to the variable _enemy.

Using the variable we just stored, the next line assigns the parent game object of each enemy to be the blank enemy container variable we assigned at the start of our script.

Lastly, we yield a new return time of 5 seconds, allowing an enemy to spawn every 5 seconds, each in a random position, and being stored in a container to save cluttering up our hierarchy.

Our final method is public, as we call it from our player script when our lives are 0 and it’s game over. This simply switches our bool to true and stops enemies from spawning repeatedly once the game ends.

On top of storing our enemies in a container. We can also do the same with our lasers. Spawning objects in Unity without clutter is essential to easier troubleshooting at runtime and in our hierarchy.

Within our player class we can add a game object variable to once again store this container. Our container simply being a blank game object within our player game object. Now within our FireLaser method, we simply store our instantiate line to a game object variable, and then access it’s parent, and have it equal our container’s game object.

--

--

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