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());
}
}
}