Up to this point, when scripting in Unity we have been using the Update method to program our logic. Update checks everything once per frame however, and can make it unnecessarily complex to program functions that take place over multiple frames. This is where coroutines come in!

Coroutines can execute game logic over multiple frames and allows you to pause a function and wait for certain conditions to be met before continuing.

This can allow you to create a series of actions to be completed in order without having to constantly check everything. Say you wanted a player to be able to click on an enemy and have the Player move to the enemy and begin attacking. This could be done by telling the Player to move towards the enemy until it collides with the enemy, then attack that enemy.

Coroutines are initiated with IEnumerator. Within this, a series of While loops dictate the logic. The While loops check for a status to be true, and if it is, run code and then use the yield command to skip the rest of the logic until the next frame. When the While loop becomes false, it skips over that piece of logic and checks the next While loop, and does this in sequence until all of the loops return false.