im working on this spawn script that is supposed to spawn the max ammount of zombie and then stop until they are gone but it spawns to many zombie than the ammount its supposed to.
it seems that it is setting the WaveMultiplier variable is setting the EnemiesInWave variable to high. but Why?
here is the code
using UnityEngine.UI;
using UnityEngine;
using System.Collections;
public class SpawnEnemies : MonoBehaviour {
public bool isStarting;
public GameObject Enemy;
public Transform[] SpawnPoints;
public GameObject[] EnemiesInMap;
public int EnemiesIn_Map;
public int EnemiesInWave = 20;
public int WaveMultiplier = 20;
public int CurWave = 1;
public Text WaveNumber;
public float startWait;
public float spawnWait;
public float waveWait;
void Start()
{
isStarting = true;
}
void Update()
{
EnemiesInMap = GameObject.FindGameObjectsWithTag("Enemy");
EnemiesIn_Map = EnemiesInMap.Length;
if (isStarting == true)
{
StartCoroutine(SpawnWaves());
isStarting = false;
}
if (EnemiesIn_Map == 0)
StartCoroutine(UpdateWave());
}
IEnumerator SpawnWaves()
{
for (int i = 0; i < EnemiesInWave; i++)
{
Transform spawnPoint = SpawnPoints[Random.Range(0, SpawnPoints.Length)].transform;
Quaternion spawnRotation = Quaternion.identity;
Instantiate(Enemy, spawnPoint.position, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
}
IEnumerator UpdateWave()
{
yield return new WaitForSeconds(waveWait);
EnemiesInWave += WaveMultiplier;
CurWave += 1;
StartCoroutine(SpawnWaves());
}
}
if (isStarting == true)
{
StartCoroutine(SpawnWaves());
isStarting = false;
}
Then as EnemiesIn_Map is equal to 0 the first time you run this, UpdateWave() is executed and inside UpdateWave() you not only increase the EnemiesInWave variable but also spawn another wave with SpawnWaves(), in this case 40 new enemies.
To avoid it your Update should look something like this:
The issue right now seems to be that if you kill all the enemies that are currently on the screen, EnemiesIn_Map would be 0 (even if we have more enemies ready to spawn) and then we start the new wave even if the old one is still running.
these modifications update the EnemiesIn_Map with all the enemies that we will spawn directly, so we must kill the whole wave before the next one can start.
Try this(not tested):
public class SpawnEnemies : MonoBehaviour {
public bool isStarting;
public bool spawningNewUnits;
public GameObject Enemy;
public Transform[] SpawnPoints;
public int EnemiesIn_Map;
public int EnemiesInWave = 20;
public int WaveMultiplier = 20;
public int CurWave = 1;
public Text WaveNumber;
public float startWait;
public float spawnWait;
public float waveWait;
void Start()
{
isStarting = true;
}
void Update()
{
if (isStarting)
{
StartCoroutine(SpawnWaves());
isStarting = false;
}
if (EnemiesIn_Map == 0 && !spawningNewUnits)
StartCoroutine(UpdateWave());
}
IEnumerator SpawnWaves()
{
EnemiesIn_Map += EnemiesInWave;
for (int i = 0; i < EnemiesInWave; i++)
{
Transform spawnPoint = SpawnPoints[Random.Range(0, SpawnPoints.Length)].transform;
Quaternion spawnRotation = Quaternion.identity;
Instantiate(Enemy, spawnPoint.position, spawnRotation);
yield return new WaitForSeconds(spawnWait);
}
}
IEnumerator UpdateWave()
{
spawningNewUnits = true;
yield return new WaitForSeconds(waveWait);
EnemiesInWave += WaveMultiplier;
CurWave += 1;
StartCoroutine(SpawnWaves());
spawningNewUnits = false;
}
}
And in your Enemy script you need a reference to this script and when the enemy dies you do EnemiesIn_Map -= 1;