How to use Coroutines?

This time we see how coroutines work. We use it to spawn a powerup on random x value and random time

Photo by Safar Safarov on Unsplash

Coroutines

This is a subject that can be dangerous sometimes.
In the last hour or so, I managed just to crash Unity with this, multiple times before I realized why that happened... :D

Coroutines are used to do something, run the code and wait for amount of seconds and run the code again.

You can start and end coroutines with these (You can also use StopAllCoroutines(); to stop all coroutines you have)

StartCoroutine(coroutine);StopCoroutine(coroutine);

I used this in the 2D Space Shooter game and I will show you how I spawned a Powerup in random X and random time between 3–7sec.

We need an IEnumerator for this and a while loop so we can end the loop when we want just by changing from let’s say false to true and the loop ends. :)

We need a boolean which specifies if we can spawn powerup or not

Then we create a private GameObject for our powerup

This is what we use to make it happen. Let’s go down and understand what does what.

IEnumerator to spawn powerups
  • We give the IEnumerator a name (SpawnPowerUp)
  • Then we create a while loop, when _stopSpawning is false, we can do the code inside the while loop.
  • We create a new Vector3 for our spawn, random on the X-axis -8f, 8f.
  • After that we Instantiate(our powerup prefab, position where we want it, rotation of the game object)
  • At the end of our while loop, we use yield return to wait for the second amount we want and then we start to run the code from the beginning again till our _stopSpawning is true
  • With Random.Range we can specify that the time we wait is random between 3–7

We add this script in our Empty GameObject (Remember to reset the transform to 0,0,0) then name the Empty GameObject SpawnManager

Then we just add our Powerup prefab and we are done! :)

When you want to use this code for example in Start function you write
StartCoroutine(SpawnPowerUp());

With this, we can’t crash unity! :)

Thank you for reading this far! Much love, and see you in the next one :)

Photo by Adi Yusuf on Unsplash

--

--

--

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Prime Number in Python — In Hindi | Python program to find a number is Prime or Not | #YouTube

Why are we here? & Fortis Oeconomia F.A.Q

From being a pretend-ninja to being an actual ninja-in-training: a story of an Ibadan boy’s journey…

Alternative to WordPress Comments

Configuring DNS For The Booking Factory Website

Accessing Prismic Releases Programmatically

The curious relationship between simplicity and top-performance

Transferet Relocation | Home Packers and Movers | Office Moving Service

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
Janika Suhonen

Janika Suhonen

From the beautiful snowy country with a touch of "good" humor? Inspired Unity Developer to learn more.

More from Medium

Local Deploy to Multiple Emulators and Simulators with Cake

CSS Principle For Project Structures

Angular JS Parent-Child Controller Communication and Best Practice for usage

How We Implemented Vehicle Mechanics in Our Game — Part 4