简体   繁体   中英

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

How can I debug the setHealth method to find out if it is being called? the player health script is not working? the player health script should count down health status and the health bar should decrease. I would like to know where should debug and how to debug in the code to see what is being called? At the moment when I play the game no syntax error but the health bar doesn't decrease and the player game object dies instantly.

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

Your error comes form the fact that you are decreasing MaxHealth instead of curHealth (in OnCollisionEnter). These are a few hints for next time.

Indent you code

It is easier to read when you post a question, and in your scripts.

Use constants

Variables that should not change over time should be declared const :

public const float max_health = 100f;

That way your compiler will tell you about the mistake.

If during the lifetime of your program it is possible that the max health changes depending on how you instanciate your class, you can also use the readonly keyword :

public readonly float max_health;

The value can be set only in the class constructor, then you cannot change it anymore.

Use a debugger

This error could have been found easily with the use of a debugging tool. Check the documentation concerning your development environment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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