【问题标题】:Killing one Enemy causes every enemy to disappear - C# Unity杀死一个敌人会导致所有敌人消失 - C# Unity
【发布时间】:2016-03-09 22:54:25
【问题描述】:

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

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

【问题讨论】:

  • 实例问题。

标签: c# unity3d scripting unity5


【解决方案1】:

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

public float Health = 10.0f;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多