I made a List of gameObjects that are currently in my box collider and it works. However, when I set a gameobject inactive,
gameObject.SetActive(false); // To Eliminate Enemy
the gameObject remains in the List without being removed. How do I remove the gameObject from a List in the OnTriggerEnter function from another class?
PlayerLifeClass:
private List<GameObject> ObjectsInRange = new List<GameObject>();
public void OnTriggerEnter(Collider collider)
{
if (collider.tag != "Player" && collider.tag == "Zombie")
{
ObjectsInRange.Add(collider.gameObject);
damage = ObjectsInRange.Count; //amount of zombies inside collider
//***Over here***
zombie.DamagePlayer(damage);
Debug.Log(damage);
}
}
public void OnTriggerExit(Collider collider)
{
if (collider.tag != "Player" && collider.tag == "Zombie")
{
//Probably you'll have to calculate which object it is
ObjectsInRange.Remove(collider.gameObject);
Debug.Log(ObjectsInRange);
}
}
ZombieClass:
public class Zombie : MonoBehaviour
{
public int currentHealth;
private Player player;
private PlayerLifeCollider playerCollider;
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//if health has fallen below zero, deactivate it
gameObject.SetActive(false); //***Over here***
}
public void DamagePlayer(int damage)
{
player.Life(damage);
}
}
If I understood what you want, Something like this would do,
PlayerLife Class
public static List<GameObject> ObjectsInRange = new List<GameObject>();
Zombie Class
public void Damage(int damageAmount)
{
//subtract damage amount when Damage function is called
currentHealth -= damageAmount;
//Check if health has fallen below zero
if (currentHealth <= 0)
{
//if health has fallen below zero, deactivate it
PlayerLife.ObjectsInRange.Remove(gameObject);
gameObject.SetActive(false); //***Over here***
//You can also do a Destroy(gameObject) if you'd like;
}
}