繁体   English   中英

如何检查调试? 为什么我的播放器健康无法正常工作?

[英]how can I check for debug? why my playerhealth is not working?

如何调试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";
    }
}

您的错误来自以下事实:您正在减少MaxHealth而不是curHealth(在OnCollisionEnter中)。 这些是下一次的一些提示。

缩进代码

在发布问题时以及在脚本中阅读起来都更容易。

使用常数

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

public const float max_health = 100f;

这样,您的编译器将告诉您有关错误的信息。

如果在程序生命周期中,最大运行状况可能根据实例化类的方式而改变,则还可以使用readonly关键字:

public readonly float max_health;

该值只能在类构造函数中设置,然后就不能再更改。

使用调试器

使用调试工具可以轻松发现此错误。 查看有关您的开发环境的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM