Stop Right There with Coroutines in Unity

When you need to wait a certain amount of time before performing an action, the coroutine in Unity will come to the rescue.The informative description below, straight from Unity’s documentation, shows that a coroutine can suspend it’s execution, until …


This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin

When you need to wait a certain amount of time before performing an action, the coroutine in Unity will come to the rescue.

The informative description below, straight from Unity’s documentation, shows that a coroutine can suspend it’s execution, until the given yield instruction finishes. Basically, scripts run from top to bottom with no pause, but a coroutine gives the ability to pause and wait for instructions before moving on.

For this example, I am going to be making a basic enemy spawner that will generate new enemies every so many seconds. I want the enemies to spawn in a while loop, meaning so long as a certain condition is true, the enemies will keep spawning until that condition is false. First comes a variable to house our condition, coming in the form of a bool (true or false). To be clear about the intentions here, the name can be an obvious stopSpawning and should be set to false. Now enemies will spawn until stopSpawning becomes true.

To create a coroutine, a function of the type IEnumerator needs to be made, followed by a suitable name for the method. Here I named it SpawnRoutine(). As far as the other text, the while loop will keep going until that stopSpawning variable becomes true, which I can assign later to when the player dies for instance. The important part to note is the last line of code. This yield return new statement is needed to keep the coroutine from throwing an error. It’s also telling the code to yield, then return something new. That new return is the last bit of code saying WaitForSeconds(2f), which tells my code to wait for 2 seconds before continuing. Since this is in a while loop, after the 2 seconds have elapsed, the code will go back to the top and create another enemy, and so on and so forth, until the player dies.

The last thing to cover is how to start a coroutine. This is very straight forward with the key words StartCoroutine, followed by the name of your method in brackets(). Another set of brackets for the method itself is needed at the end of the method name to call it like you would any other void method. Here I put it in void start, so this will activate when the game begins.

Coroutines are powerful functions when it comes to coding techniques. There are a whole lot of instances where needing to pause and use a timer in a project, will be critical. For those cases when we need to yield…coroutines to the rescue!


Stop Right There with Coroutines in Unity was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Jared Amlin


Print Share Comment Cite Upload Translate Updates
APA

Jared Amlin | Sciencx (2021-05-12T17:01:59+00:00) Stop Right There with Coroutines in Unity. Retrieved from https://www.scien.cx/2021/05/12/stop-right-there-with-coroutines-in-unity/

MLA
" » Stop Right There with Coroutines in Unity." Jared Amlin | Sciencx - Wednesday May 12, 2021, https://www.scien.cx/2021/05/12/stop-right-there-with-coroutines-in-unity/
HARVARD
Jared Amlin | Sciencx Wednesday May 12, 2021 » Stop Right There with Coroutines in Unity., viewed ,<https://www.scien.cx/2021/05/12/stop-right-there-with-coroutines-in-unity/>
VANCOUVER
Jared Amlin | Sciencx - » Stop Right There with Coroutines in Unity. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/12/stop-right-there-with-coroutines-in-unity/
CHICAGO
" » Stop Right There with Coroutines in Unity." Jared Amlin | Sciencx - Accessed . https://www.scien.cx/2021/05/12/stop-right-there-with-coroutines-in-unity/
IEEE
" » Stop Right There with Coroutines in Unity." Jared Amlin | Sciencx [Online]. Available: https://www.scien.cx/2021/05/12/stop-right-there-with-coroutines-in-unity/. [Accessed: ]
rf:citation
» Stop Right There with Coroutines in Unity | Jared Amlin | Sciencx | https://www.scien.cx/2021/05/12/stop-right-there-with-coroutines-in-unity/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.