Hey guys so here's an outline of my problem using a basic 3-step approach
The Problem Defined Waves 1-22 should spawn in order with the correct paths and enemy prefabs. However, Waves 1-4 only spawn and repeat on a loop. https://www.youtube.com/watch?v=p0HZ_qo4SxU
Possible Causes I think the issue possibly lies within my enemyspawner.cs script but it could also potentially lie within my level.cs and waveconfig.cs scripts. Oddly enough this seems to also be an issue with the original source code as well. I made several copies of the prefabs, gave them unique paths and waves and changed the new enemy prefabs to different colors to test it and the same result happens. https://www.youtube.com/watch?v=8zr7CVjty6k
The needed solution I need all 22 waves to re-spawn in order correctly as only having 4 waves repeat is very repetitive and boring.
Here is my specific code for enemyspawner:
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;
IEnumerator Start()
{
do
{
yield return StartCoroutine(SpawnAllWaves());
}
while (looping);
}
private IEnumerator SpawnAllWaves()
{
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.GetWaypoints()[0].transform.position,
Quaternion.identity);
newEnemy.GetComponent<EnemyPathing>().SetWaveConfig(waveConfig);
yield return new WaitForSeconds(waveConfig.GetTimeBetweenSpawns());
}
}
}
Here is my specific code for waveconfig:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(menuName = "Enemy Wave Config")]
public class WaveConfig : ScriptableObject
{
[SerializeField] GameObject enemyPrefab;
[SerializeField] GameObject pathPrefab;
[SerializeField] float timeBetweenSpawns = 0.5f;
[SerializeField] float spawnRandomFactor = 0.3f;
[SerializeField] int numberOfEnemies = 5;
[SerializeField] float moveSpeed = 2f;
public GameObject GetEnemyPrefab() { return enemyPrefab; }
public List<Transform> GetWaypoints()
{
var waveWaypoints = new List<Transform>();
foreach (Transform child in pathPrefab.transform)
{
waveWaypoints.Add(child);
}
return waveWaypoints;
}
public float GetTimeBetweenSpawns() { return timeBetweenSpawns; }
public float GetSpawnRandomFactor() { return spawnRandomFactor; }
public int GetNumberOfEnemies() { return numberOfEnemies; }
public float GetMoveSpeed() { return moveSpeed; }
}
Here is my specific code for Level:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Level : MonoBehaviour
{
[SerializeField] float delayInSeconds = 2f;
public void LoadStartMenu()
{
SceneManager.LoadScene(0);
}
public void LoadGame()
{
SceneManager.LoadScene("Game");
FindObjectOfType<GameSession>().ResetGame();
}
public void LoadGameOver()
{
StartCoroutine(WaitAndLoad());
}
IEnumerator WaitAndLoad()
{
yield return new WaitForSeconds(delayInSeconds);
SceneManager.LoadScene("Game Over");
}
public void QuitGame()
{
Application.Quit();
}
}
yield return StartCoroutine(SpawnAllEnemiesInWave(currentWave));
, you can justyield return SpawnAllEnemiesInWave(currentWave);
: the function is already returning an IEnumerator, which is the type of object expected by ayield
instruction. \$\endgroup\$ – Draco18s Feb 6 at 4:32