Unity:如何从列表中删除不活动的游戏对象
Unity: How to remove inactive gameObject from List
我制作了一个当前在我的盒子对撞机中的游戏对象列表并且它有效。但是,当我将游戏对象设置为非活动状态时,
gameObject.SetActive(false); // To Eliminate Enemy
游戏对象保留在列表中而没有被删除。如何从另一个 class?
的 OnTriggerEnter 函数中的列表中删除游戏对象
玩家生命类:
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);
}
}
你还有很多方法,我教你一个简单的。
修改你的僵尸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);
}
或类似的东西,但我认为这不是一个好的选择。
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);
}
或使用单例。 向 PlayerLife 添加新变量Class:
...
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);
}
如果我明白你想要什么,像这样就可以了,
玩家生命Class
public static List<GameObject> ObjectsInRange = new List<GameObject>();
僵尸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;
}
}
我制作了一个当前在我的盒子对撞机中的游戏对象列表并且它有效。但是,当我将游戏对象设置为非活动状态时,
gameObject.SetActive(false); // To Eliminate Enemy
游戏对象保留在列表中而没有被删除。如何从另一个 class?
的 OnTriggerEnter 函数中的列表中删除游戏对象玩家生命类:
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);
}
}
你还有很多方法,我教你一个简单的。
修改你的僵尸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);
}
或类似的东西,但我认为这不是一个好的选择。
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);
}
或使用单例。 向 PlayerLife 添加新变量Class:
...
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);
}
如果我明白你想要什么,像这样就可以了,
玩家生命Class
public static List<GameObject> ObjectsInRange = new List<GameObject>();
僵尸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;
}
}