I have a list of enemies (GameObjects). I want a door to be locked until they are all dead. I am using a public list of the enemies (so I can reuse this concept later in the game).
public GameObject fightBarrier;
public List<GameObject> enemies = new List<GameObject>();
public SpriteRenderer Barrier_0;
public SpriteRenderer Barrier_1;
public SpriteRenderer Barrier_2;
public SpriteRenderer Barrier_3;
public Sprite Barrier;
public Sprite noBarrier;
private int enemiesAmountCheck;
// overrided because it's inheriting from another script
protected override void OnCollide(Collider2D coll)
{
if (coll.name == "Player")
{
enemiesAmountCheck = enemies.Count;
for (int i = 0; i < enemies.Count - 1; i )
{
// This if statement has to check if the GameObject doesn't exist (AKA the GameObject is None)
if (enemies[i]... )
{
enemiesAmountCheck = enemiesAmountCheck - 1;
}
}
if (enemiesAmountCheck > 0)
{
fightBarrier.GetComponent<BoxCollider2D>().enabled = true;
Barrier_0.sprite = Barrier;
Barrier_1.sprite = Barrier;
Barrier_2.sprite = Barrier;
Barrier_3.sprite = Barrier;
}
else
{
fightBarrier.GetComponent<BoxCollider2D>().enabled = false;
Barrier_0.sprite = noBarrier;
Barrier_1.sprite = noBarrier;
Barrier_2.sprite = noBarrier;
Barrier_3.sprite = noBarrier;
}
}
}
CodePudding user response:
After you destroy a GameObject, the index in the array should become null. So, you could say if(enemies[i]==null)
, and that should return true if the enemy object has been destroyed.
CodePudding user response:
Yep!
for(int x = 0; x < enemies.Count; x ) // Loop through list
{
if (enemies[x] == null)
{
enemies.RemoveAt(x);//When object is destroyed it will be set null
}
}