C# for Unity: Coroutines & Multiplayer Networking

Lesson 3 of 7

Advanced Coroutine Patterns

Real coroutines usually need more control than a single loop: waiting on conditions, nesting, and stopping cleanly.

IEnumerator SpawnWave(int count)
{
    for (int i = 0; i < count; i++)
    {
        Instantiate(enemyPrefab, RandomSpawnPoint(), Quaternion.identity);
        yield return new WaitForSeconds(1.5f);
    }
}

IEnumerator WaitForPlayerReady()
{
    yield return new WaitUntil(() => player.IsReady);
    Debug.Log("Player ready, starting match");
}

Useful yield instructions

InstructionWaits for
yield return nullnext frame
yield return new WaitForSeconds(t)t seconds, scaled by Time.timeScale
yield return new WaitForSecondsRealtime(t)t real seconds, ignores pause
yield return new WaitUntil(() => condition)condition to become true
yield return StartCoroutine(Other())a nested coroutine to finish

Stopping coroutines

Coroutine handle = StartCoroutine(SpawnWave(5));
...
StopCoroutine(handle);   // stop a specific one
StopAllCoroutines();      // stop everything running on this MonoBehaviour

A coroutine also stops automatically if the GameObject it's running on is destroyed or disabled.

📝 Advanced Coroutines Quiz

Passing score: 70%
  1. 1.Which yield instruction pauses a coroutine until a condition becomes true?

  2. 2.____() stops every coroutine currently running on a MonoBehaviour.

  3. 3.A running coroutine keeps executing even after the GameObject it belongs to is destroyed.