Una Coroutine permite ejecutar código repartido a lo largo del tiempo sin bloquear el juego.

Necesita:

using System.Collections;

Coroutine básica

IEnumerator Esperar()
{
    Debug.Log("Inicio");

    yield return new WaitForSeconds(2f);

    Debug.Log("Pasaron 2 segundos");
}

Ejecutar:

StartCoroutine(Esperar());

WaitForSeconds

yield return new WaitForSeconds(1f);

Es afectado por Time.timeScale.


WaitForSecondsRealtime

yield return new WaitForSecondsRealtime(1f);

No depende de timeScale.


Esperar un frame

yield return null;

Repetir indefinidamente