BenSmith
BenSmith

Reputation: 431

Unity: How to remove inactive gameObject from List

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);
      }
   }

Upvotes: 0

Views: 3525

Answers (2)

nipercop
nipercop

Reputation: 387

You have more ways, i'll show you an easy.

Modificate your Zombie Class:

public bool Damage(int damageAmount)
{
    currentHealth -= damageAmount;
    if (currentHealth <= 0)
    {
        gameObject.SetActive(false); //***Over here***
        return true; 
    }
    return false;
}

and PlayerLifeClass:

if (collider.tag != "Player" && collider.tag == "Zombie"){
    ObjectsInRange.Add(collider.gameObject);
    damage = ObjectsInRange.Count; //amount of zombies inside collider
    if(collider.GetComponent<Zombie>().Damage(damage)){
        // if zombie dead, remove him.
        ObjectsInRange.Remove(collider.gameObject);
    }
    Debug.Log(damage);
}

Or somithing like that, but it not good choice, i think.

void Start(){
   playerLifeClass = GameObject.FindGameObjectWithTag("Player")>GetComponent<PlayerLifeClass>();
}
public bool Damage(int damageAmount)
{
    currentHealth -= damageAmount;
    if (currentHealth <= 0)
    {
    //if health has fallen below zero, deactivate it 
       gameObject.SetActive(false); //***Over here***
       playerLifeClass.RemoveZombie(gameObject);
    }
}

And in PlayerLifeClass add new Method:

public void RemoveZombie(GameObject zomb){
   ObjectsInRange.Remove(zomb);
}

Or use Singletons. Add new variable to PlayerLifeClass:

...
public static PlayerLifeClass instance;
public List<GameObject> ObjectsInRange = new List<GameObject>();
...

void Start(){
    instance = this;
    ...
}

And Use it in Zombie.cs:

 if (currentHealth <= 0){
    gameObject.SetActive(false); //***Over here***
    PlayerLifeClass.instance.ObjectsInRange.Remove(gameObject);
 }

Upvotes: 2

Suraj S
Suraj S

Reputation: 1019

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;

        }
    }

Upvotes: 2

Related Questions