Hi I have this code for the enemies:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyEngine : MonoBehaviour {
//*********Member Variables**********//
public float enemyHealth = 100.0f;
//*****List****//
public List<GameObject> allEnemies;
public Transform selectedEnemy;
private Transform enemyTransform;
private bool enemyIsAlive;
void Start () {
enemyIsAlive = true;
allEnemies = new List<GameObject>();
selectedEnemy = null;
enemyTransform = transform;
AddEnemies();
}
//What these next two functions do is adds all the enemies in the scene into a list.
void AddEnemies()
{
GameObject[] zombieEnemy = GameObject.FindGameObjectsWithTag("zombie");
GameObject[] mummyEnemy = GameObject.FindGameObjectsWithTag("mummy");
foreach(GameObject enemy in zombieEnemy)
{
AddInList(enemy.gameObject);
}
foreach(GameObject mummy in mummyEnemy)
{
AddInList(mummy.gameObject);
}
}
void AddInList(GameObject enemy)
{
allEnemies.Add (enemy);
}
void RemoveNull()
{
foreach(GameObject nullOBJ in allEnemies)
{
if(nullOBJ.gameObject == null)
{
allEnemies.Remove(nullOBJ.gameObject);
}
}
}
bool IsEnemyDead()
{
return enemyIsAlive;
}
// Update is called once per frame
void Update ()
{
if(IsEnemyDead())
{
if(enemyHealth <= 0)
{
enemyIsAlive = false;
}
else
{
}
}
else
{
allEnemies.Remove(this.gameObject);
Debug.Log (allEnemies.Count);
Destroy(this.gameObject);
}
}
}
Any idea of how to do this properly. Thanks.
Edit: The script is placed individually on each enemy. What i have seen is that if i don’t destroy the object and i just remove the item from the list then it updates and removes it, however on the other enemies it is still on the list.
EDIT: I managed to do it. What i needed to do was check the gameobjects from zombieenemy and mummyenemy, and then if it is null then remove it from the point in allEnemies