Hop into Unity with Coroutines

Mike Brisson
2 min readApr 4, 2021

--

There are many times that you want to delay a process and one of the most efficient ways to achieve this is with coroutines.

The first step is to declare an IENumerator method:

Anything you want to delay will go within this method. If you want to delay a fire rate, you can do that with a “WaitForSeconds” variable and having the code “yield” until that time has passed:

Here, Fire() won’t be called until 0.5 seconds have passed

IENumerators don’t get called like regular methods. They are coroutines and need to be called with the “StartCoroutine()” call:

If you need to stop a routine, you will need to call the method withing quotes.

If you want a coroutine to run indefinitely, you would call it within the Start() mentod and have a while statement to keep it running:

This was the code used to create the title gif. Here you can see the JumpRoutine() called within StartCoroutine(). The JumpRoutine() itself is set for a neverending loop that will restart every 2 seconds.

--

--

No responses yet