Running Coroutines simultaneously

I have my code more or less copied over to try to run asteroids along side the enemies, however with the last change in the lecture to make the coroutines loop they now run one after the other. Is there a way to still tie it together like the instructor has it while keeping them working at the same time? here is my code so far.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class EnemySpawner : MonoBehaviour

{

[SerializeField] List<WaveConfig> waveConfigs;

[SerializeField] int startingWave = 0;

[SerializeField] bool looping = false;

// Start is called before the first frame update

IEnumerator Start()

{

    do

    {

    yield return StartCoroutine(SpawnAllEnemyWaves());

    yield return StartCoroutine(SpawnAllAsteroidWaves()); 

    }

    while(looping);

}

private IEnumerator SpawnAllEnemyWaves()

{

    for(int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)

    {

        var currentWave = waveConfigs[waveIndex];

        yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave));

    }

}

private IEnumerator SpawnAllEnemiesInWave(WaveConfig waveConfig)

{

    for (int enemyCount = 0; enemyCount < waveConfig.GetNumberOfEnemies(); enemyCount++)

    {

        var newEnemy = Instantiate(

        waveConfig.GetEnemyPrefab(), 

        waveConfig.GetEnemyWaypoints()[0].transform.position,

        Quaternion.identity);

        newEnemy.GetComponent<EnemyPathing>().SetEnemyWaveConfig(waveConfig);

        yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());

    }

}

private IEnumerator SpawnAllAsteroidWaves()

{

    for(int waveIndex = startingWave; waveIndex < waveConfigs.Count; waveIndex++)

    {

        var currentWave = waveConfigs[waveIndex];

        yield return StartCoroutine(SpawnAllAsteroidsInWave(currentWave));

    }

}

private IEnumerator SpawnAllAsteroidsInWave(WaveConfig waveConfig)

{

    for (int asteroidCount = 0; asteroidCount < waveConfig.GetNumberOfAsteroids(); asteroidCount++)

    {

        var newAsteroid = Instantiate(

        waveConfig.GetAsteroidPrefab(),

        waveConfig.GetAsteroidWaypoints()[0].transform.position,

        Quaternion.identity);

        newAsteroid.GetComponent<AsteroidPathing>().SetAsteroidWaveConfig(waveConfig);

        yield return new WaitForSeconds(waveConfig.GetTimeBetweenAsteroidSpawns());

    }

}

}

Hi John,

What is the problem you like to solve? At the moment, I don’t know what you mean by “still tie it together […] while keeping them working at the same time”.

well id like to get them to run at the same time and not one after the other, so i have enemies and asteroids flying around at the same time.

In that case, don’t make the Start method a coroutine. At the moment, due to the yield instruction, the first coroutine gets started, and when it’s done, the next one gets started. Instead, make the Start method a normal method and start the two coroutines via the normal method call. In the SpawnAllEnemyWaves() and the SpawnAllAsteroidWaves() methods, you define a loop and, if you don’t want to create an endless loop, also an exit condition.

Thank you, so I just keep the loops separate to let them run side by side.

Your approach is correct. Coroutines are able to run independently. The problem in your original code is that the start of each coroutine happens in another coroutine: Start(). To put it simply, the code in methods get executed line by line, and each command must be executed before the next one can be executed. That’s why there is a huge delay between the start of each coroutine.

Your coroutine themselves might be fine as they are. You just have to check if the SpawnAll*Waves methods really spawn all waves or if the Start method contained a part of the execution logic. In that case, you would have to implement that logic in your respective SpawnAll*Waves method.

Hopefully, this made sense. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms