This script is supposed to spawn in my zombies in waves. My problem is that it spawns in the second wave when it isn’t supposed to and I don’t know why this is doing this. I know that enemyCount isn’t 0 because when the game ran I checked and confirmed that it is over 0. Is it possible for someone to have a look at the code and tell me whats wrong. I haven’t been able to find anything. Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waves : Zombie
{
public int enemyCount;
public GameObject behemoth;
public GameObject runner;
public GameObject zombie;
private int xPos;
private bool HasSecondWaveRan;
private bool HasThirdWaveRan;
public int enemyCount2 = 1;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < 10; i++)
{
xPos = Random.Range(31, -14);
Instantiate(zombie, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
enemyCount = enemyCount + 1;
}
for (int i = 0; i < 3; i++)
{
xPos = Random.Range(31, -14);
Instantiate(runner, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
enemyCount = enemyCount + 1;
}
}
// Update is called once per frame
void Update()
{
if (enemyCount == 0)
{
StartCoroutine(SecondWave());
HasSecondWaveRan = true;
}
if (enemyCount2 == 0)
{
StartCoroutine(ThirdWave());
HasThirdWaveRan = true;
}
}
IEnumerator SecondWave()
{
if (HasSecondWaveRan == false)
{
yield return new WaitForSeconds(5.0f);
for (int i = 0; i < 5; i++)
{
xPos = Random.Range(31, -14);
Instantiate(runner, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
xPos = Random.Range(31, -14);
Instantiate(zombie, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
enemyCount2 = 25;
enemyCount = 1;
HasSecondWaveRan = true;
}
}
IEnumerator ThirdWave()
{
if (HasThirdWaveRan == false)
{
yield return new WaitForSeconds(5.0f);
for (int i = 0; i < 5; i++)
{
xPos = Random.Range(31, -14);
Instantiate(runner, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
for (int i = 0; i < 20; i++)
{
xPos = Random.Range(31, -14);
Instantiate(zombie, new Vector3(xPos, 0.4f, 6), Quaternion.identity);
}
HasThirdWaveRan = true;
}
}
}