Hey so im starting a coroutine when i start my game and when there is enough enemies I stop it and then I can click on a button to start spawning enemies again the coroutine wont start can someone help me? here is my script
public void NextWaveButton()
{
currentWaveNumber++;
maxNumberOfEnemies *= 2;
currentNumberOfEnemies = 0;
StartCoroutine (es);
nextWaveButton.SetActive(false);
}
IEnumerator SpawnRandomEnemy()
{
WaitForSeconds wait = new WaitForSeconds(spawnInterval);
while (currentNumberOfEnemies < maxNumberOfEnemies)
{
enemiesLeft = maxNumberOfEnemies;
int enemyIndex = Random.Range(0, enemyPrefabs.Length);
enemyRandomSpawnPoint = Random.Range(0, spawnPoints.Length);
GameObject newEnemy = Instantiate(enemyPrefabs[enemyIndex], spawnPoints[enemyRandomSpawnPoint].transform.position, enemyPrefabs[enemyIndex].transform.rotation);
newEnemy.transform.parent = enemyContainer.transform;
currentNumberOfEnemies++;
yield return wait;
}
if (currentNumberOfEnemies == maxNumberOfEnemies)
{
Debug.Log("Max number of enemies spawned so stopped spawning more enemies");
StopCoroutine(es);
}
}