Wavespawner tutorial: infinite wave problem

Updated on May 27, 2018 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
0 on May 27, 2018

It spits out enemies infinetly!!

I tried following the tutorial as best as i could but i could not find the error, pls help

using System.Collections;
using UnityEngine;
public class WaveSpawner : MonoBehaviour {
#region variables
public enum SpawnState { SPAWNING, WAITING, COUNTING };
[System.Serializable]
public class wave
{
public string name;
public Transform enemy;
public int count;
public float rate;
}
public wave[] waves;
private int nextWave = 0;
public Transform[] spawnPoints;
private float TimeBetweenWaves = 5f;
public float waveCountdown;
private float searchCountdown = 1f;
private SpawnState state = SpawnState.COUNTING;
#endregion
#region Unityshit
void Start()
{
waveCountdown = TimeBetweenWaves;
}
void Update()
{
if (state == SpawnState.WAITING)
{
//check if enemies are still alive
if (!EnemyIsAlive())
{
//Begin a new round
WaveCompleted();
}
}
if (waveCountdown <= 0)
{
if (state != SpawnState.SPAWNING)
{
//start spawing
StartCoroutine(SpawnWave(waves[nextWave])); 
}
}
else
{
waveCountdown -= Time.deltaTime;
}
}
private bool EnemyIsAlive()
{
searchCountdown -= Time.deltaTime;
if (searchCountdown <= 0f)
{
searchCountdown = 1f;
if (GameObject.FindGameObjectWithTag("Enemy") == null)
{
return false;
}
}
return true;
}
IEnumerator SpawnWave(wave _wave)
{
state = SpawnState.SPAWNING;
//spawn
for (int i = 0; i < _wave.count; i++)
{
SpawnEnemy(_wave.enemy);
yield return new WaitForSeconds(1f / _wave.rate);
}
state = SpawnState.WAITING;
yield break;
}
#region spawing Enemies
void SpawnEnemy (Transform enemy)
{
Transform _sp = spawnPoints[Random.Range(0, spawnPoints.Length)];
//spawn enemy
Instantiate(enemy, _sp.position, _sp.rotation);
Debug.Log("Spawning Enemy: " + enemy.name);
}
#endregion
void WaveCompleted()
{
state = SpawnState.COUNTING;
waveCountdown = TimeBetweenWaves;
if (nextWave + 1 > waves.Length -1)
{
nextWave = 0;
Debug.Log("All WAVES COMPLETE...");
}
else
{
nextWave++;
}
}
#endregion
}
  • Liked by
Reply