- Home /
Question by
Thor625 · Jun 21, 2013 at 03:39 PM ·
javascriptlistenemysystem.collections.generic
Removing Missing Objects From A List
Hello, I have a list:
public var enemies : List.<GameObject> = new List.<GameObject>();
That I put all of the enemies in a certain area in
var hitColliders : Collider[] = Physics.OverlapSphere (transform.position, circleRadius);
for (var hit : Collider in hitColliders) {//Converts Collider[] to Collider
if(hit.gameObject.tag == "Killable"){//Detects if it is an enemy.
if(EnemyScript.added == false){//So it will not add a second time.
enemies.Add(hit.gameObject);
EnemyScript.added = true;
}
}
In this list I get the number of enemies in the area, so I will know to spawn them in if it is less than the allotted amount.
EnemiesInSection = enemies.Count;
The Problem is that when a enemy dies, in the list, the game object comes up missing, where I would like to remove it.
I have tried putting a boolean in my enemy script that when it dies it comes up true and then use enemies.remove(hit.gameObject) and that did not work.
I also tried doing this:
if(hit.gameObject == null){
enemies.Remove(hit.gameObject);
}
How would I go about removing the enemies once they die.
All help is appreciated, Thanks
Thor
Comment
Best Answer
Answer by Slobdell · Jun 21, 2013 at 04:06 PM
You are probably destroying the enemy gameobject before removing it from the list, remove it from the list first.