杀死一个敌人会导致所有敌人消失-C#统一

本文关键字:敌人 消失 统一 -C# 一个 | 更新日期: 2023-09-27 18:25:47

我在Unity中创建的游戏有问题。玩家控制一个被一群僵尸攻击的角色。我为所有僵尸创建了一个spawner,效果很好,唯一的问题是,一旦玩家杀死一个僵尸,所有僵尸都会从游戏世界中消失。我已经发布了敌人的脚本,附在下面的每个僵尸身上。我不明白为什么每个僵尸都被摧毁了,而不仅仅是一个被攻击的僵尸。任何帮助都会很棒!

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

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

杀死一个敌人会导致所有敌人消失-C#统一

问题是您已经将Health声明为静态变量。这意味着Health在所有敌人实例中都具有相同的值。像这样声明健康状况:

public float Health = 10.0f;

这样,每个实例化的敌人将能够拥有自己唯一的Health值。