I am creating a castle defense game where the enemy regulary spawns (upto limit of 24 ). After the enemy reaches 24, I want to stop creating clones. So far, I have been successful but I want to make sure that if the enemy reaches 23, it starts instantiating upto 24. How?
public GameObject[] spawnPosition;
public GameObject enemy;
[SerializeField]
private float startTime;
private float timeForSpawn;
public float speed;
public float enemyLimit;
public List<GameObject> enemyList;
public bool stopInstantiating;
void Start () {
stopInstantiating = false;
enemyList = new List<GameObject> ();
enemyLimit = 24f;
timeForSpawn = startTime;
}
void Update () {
Spawn ();
}
void Spawn () {
timeForSpawn -= Time.deltaTime;
if (timeForSpawn <= 0 && stopInstantiating == false) {
timeForSpawn = startTime;
for (int i = 0; i <= 2; i++) {
Instantiate (enemy, spawnPosition [i].transform.position, Quaternion.identity);
if (enemy != null&& stopInstantiating == false) {
enemyList.Add (enemy);
if (enemyList.Count >= enemyLimit) {
stopInstantiating = true;
}
}
}
}
}
}
I see that the list doesn't update itself as I delete the gameObjects in game. How to make sure that list updates and work on that.