消灭一个敌人会导致所有敌人消失 - C# Unity

3

我正在使用Unity创建一个游戏,遇到了问题。玩家控制的角色正在受到一群僵尸的攻击。我已经为所有僵尸创建了一个生成器,并且它运作良好,唯一的问题是,一旦玩家杀死一个僵尸,所有僵尸都会从游戏世界中消失。我在下面发布了附加到每个僵尸的敌人脚本。我无法弄清楚为什么每个僵尸都被销毁,而不仅仅是被攻击的那个。任何帮助都将是极好的!

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

public static float Damage = 10.0f;
public static float Health = 10.0f;

public Transform target;
public float Speed;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

    //Destroy the enemy if it's health reaches 0
    if(Health <= 0){
        Destroy(this.gameObject);
        Debug.Log ("Enemy Destroyed!");
    }

    //Constantly move the enemy towards the centre of the gamespace (where the base is)
    float step = Speed * Time.deltaTime;
    transform.position = Vector3.MoveTowards(transform.position, target.position, step);
  }
}

场景的设置方式是,我有一个空的游戏对象,其中包含一系列位置对象和一个生成器脚本,将敌人精灵放置到位置对象中。这一切似乎都很正常,但我找不到导致它们全部消失的原因。


实例问题。 - Fᴀʀʜᴀɴ Aɴᴀᴍ
1个回答

5
问题在于您已将 Health 声明为静态变量。这意味着 Health 在所有敌人实例之间具有相同的值。请改为这样声明健康:
public float Health = 10.0f;

这样,每个实例化的敌人都可以拥有自己独特的Health值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接