Unity:如何从List中删除不活动的gameObject

时间:2017-07-17 02:49:04

标签: c# unity3d triggers gameobject

我制作了一个当前在我的盒子对撞机中的游戏对象列表并且它有效。但是,当我将游戏对象设置为无效时,

  

gameObject.SetActive(假); //消灭敌人

游戏对象保留在列表中而不被删除。如何从另一个类的OnTriggerEnter函数中的List中删除gameObject?

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

2 个答案:

答案 0 :(得分:2)

你有更多的方法,我会告诉你一个简单的方法。

修改你的Zombie Class:

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

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

或像那样somithing ,但我觉得这不是一个好选择。

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

在PlayerLifeClass中添加新方法:

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

或使用单身人士。向PlayerLifeClass添加新变量:

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

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

并在Zombie.cs中使用它:

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

答案 1 :(得分:1)

如果我理解了你想要的东西,就会有这样的东西,

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;

        }
    }