Another yield WaitForSeconds C# question

I find yield WaitForSeconds in Javascript understandable, but when it comes to C# it’s quite confusing.
I have an enemy spawner script that I made in Javascript originally, but want to convert to C#.

function SpawnEmemies() {
  yield WaitForSeconds(initialDelay);
  for (var i : int = 0; i < waves; i++) {
    for (var j : int = 0; j < enemies; j++) {
      Instantiate(enemy, transform.position, Quaternion.identity);
      yield WaitForSeconds(timeBetweenEnemies);
    }
    yield WaitForSeconds(timeBetweenWaves);
  }
}

I found this solution more elegant than working on time buffers in the Update function. However, how would this look in C#? The main difference I noticed is that in Javascript the code actually waits after a yield WaitForSeconds, while in C# it just continues, and you have to start messing around with Coroutines and what not.

Well I guess it wasn’t that hard after all once I tried some more. Posting the answer here incase someone else wonders :slight_smile:

void Start() {
  StartCoroutine(SpawnEmemies());
}

IEnumerator SpawnEmemies() {
  yield return new WaitForSeconds(initialDelay);
  for (int i = 0; i < waves; i++) {
    for (int j = 0; j < enemies; j++) {
      Instantiate(enemy, transform.position, Quaternion.identity);
      yield return new WaitForSeconds(timeBetweenEnemies);
    }
    yield return new WaitForSeconds(timeBetweenWaves);
  }
}

Case closed.