C# 如何检查调试?为什么我的playerhealth不起作用?

C# 如何检查调试?为什么我的playerhealth不起作用?,c#,unity3d,C#,Unity3d,如何调试setHealth方法以确定是否正在调用它?玩家健康脚本不工作?玩家健康脚本应该倒计时健康状态,健康栏应该减少。我想知道应该在哪里调试以及如何在代码中调试以查看调用了什么?在我玩游戏时,没有语法错误,但是健康栏没有减少,玩家游戏对象立即死亡 using UnityEngine; using UnityEngine.UI; using System.Collections; public class PlayerHealth : MonoBehaviour { [Serializ

如何调试setHealth方法以确定是否正在调用它?玩家健康脚本不工作?玩家健康脚本应该倒计时健康状态,健康栏应该减少。我想知道应该在哪里调试以及如何在代码中调试以查看调用了什么?在我玩游戏时,没有语法错误,但是健康栏没有减少,玩家游戏对象立即死亡

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerHealth : MonoBehaviour
{
    [SerializeField] GameObject deathFX;
    [SerializeField] Transform parent;
    public Image Bar;
    public Text Text;
    public float max_health = 100f;
    public float cur_health = 0f;


    //Use this for initialization
    void Start()
    {
        // Initialize the health that is given
        cur_health = max_health;
        InvokeRepeating("decreaseHealth", 0f, 2f);
    }

    void Update()
    {
        if (cur_health <= 0)
        {
            Destroy(gameObject);   //if the player has no health point left, destroy the player.
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.GetComponent<Projectiles>())
        {
            max_health -= cur_health;
            //if the collision object has a homing script, minus player health by   damageToPlayer
        }
    }

    void decreaseHealth()
    {
        //Subtract the health at the following rate
        //Check if the health is 0 before we do any damage
        if (cur_health < 0)
        {
            cur_health = 0;
        }
        //make a new variable and divide the current health my the maximum health
        //this is because the fill value goes from 0 to 1
        float calc_health = cur_health / max_health; // 70 / 100 = 0.7
        SetHealth(calc_health);

        //Change the color of the health bar
        // on the scale of 1000, if health <= 625 and is greater    than 345, do the following
        if (cur_health != 0 && cur_health <= max_health / 1.6 && cur_health >    max_health / 2.9) 
        {
            Bar.color = new Color32(171, 162, 53, 255);
        }
        else if (cur_health != 0 && cur_health <= max_health / 2.9) // on the    scale of 1000, if health <= 625, do the following
        {
            Bar.color = new Color32(158, 25, 25, 255);
        }
    }

    void SetHealth(float myHealth)
    {
        //defill the bar based on the current health
        Bar.fillAmount = myHealth;

        //change the text to display the amount of health
        Text.text = cur_health.ToString("f0") + "/100";
    }
}
使用UnityEngine;
使用UnityEngine.UI;
使用系统集合;
公共级玩家健康:单一行为
{
[SerializeField]游戏对象死亡fx;
[序列化字段]转换父项;
公众形象栏;
公共文本;
公共浮子最大健康度=100f;
公共浮点数cur_health=0f;
//用于初始化
void Start()
{
//初始化给定的运行状况
当前健康=最大健康;
调用重复(“降低健康”,0f,2f);
}
无效更新()
{

如果(cur_health您的错误是由于您正在减少MaxHealth而不是curHealth(在OnCollisionCenter中)。以下是下一次的一些提示

缩进您的代码

当你发布一个问题时,在你的脚本中更容易阅读

使用常量

应声明不应随时间变化的变量
const

public const float max_health = 100f;
这样你的编译器就会告诉你这个错误

如果在程序的生命周期内,最大运行状况可能会因实例化类的方式而发生变化,则还可以使用
只读
关键字:

public readonly float max_health;
只能在类构造函数中设置该值,然后不能再更改它

使用调试器


使用调试工具可以很容易地找到此错误。请查看有关您的开发环境的文档。

您可以通过…呃…使用调试器进行调试…?()(此外,如果您使用的是Visual Studio:,这些可能值得一读)谢谢你的反馈,我正在修复它。这对const来说是个好主意。不过,可能会有一些时候,通过升级,最大运行状况会发生变化。