无法在Unity中获得我当前的健康状况

时间:2018-04-17 21:05:20

标签: c# unity3d

我最近在CharacterStats课上遇到了问题。我设置了两个花车。 CurrentHealthMaxHealth。在Awake(),我希望CurrentHealth等于MaxHealth,并且一旦受到伤害就减少,但它刚刚等于零。

当我开始时它起作用,但是在添加更多场景和更多游戏对象后几天后它停止工作。是否有我遗漏的东西,或者这可能是Unity中的错误?

using UnityEngine;

public class CharacterStats : MonoBehaviour
{

    public int MaxHealth = 100;
    public int CurrentHealth { get; private set; }
    public Stat damage;
    // public Stat armor;For later game updates
    void Awake()
    {
        CurrentHealth = MaxHealth;

    }
    void Update()
    {
        /* if (Input.GetKey(KeyCode.Mouse0))
         {
             TakeDamage(10);
         }*/
    }
    public void TakeDamage(int damage)
    {
        //damage -= armor.GetValue();
        damage = Mathf.Clamp(damage, 0, int.MaxValue);
        CurrentHealth -= damage;
        Debug.Log(transform.name + "takes" + damage + "damage.");

        if (CurrentHealth <= 0)
        {
            Die();
        }
    }
    public virtual void Die()
    {


        //Overrride
        Debug.Log(transform.name + "You died.....sad");
    }
}

0 个答案:

没有答案