Possible Unity Bug While Making Player Health

So I’m having a problem on something that should be extremely simple but there hasn’t been anything able to fix this, not even my programming teacher could see what’s wrong, wich makes me believe this is a problem related to unity, not my code.

I have a health system that works perfectly on enemies, and by using the same logic for the player health it simply doesn’t work, it’s like the int for the current health has two values at the same time, when I try to set it to the max health it doesn’t work at all, I tried in many different ways, but I can’t set it’s value at the beggining, wich means I start the game with the amount of health I had when I last stopped playing. But at the same time my player can die and it registers the damage somehow, but when I use Debug.Log to display my current health it only shows my maximum health (as if the current health was actually successfuly set to max when starting, but again it doesn’t matter because that’s not the real health, the real one is the amount I had last time I stopped playing) and when the player dies it suddently turns into zero, with nothing between max health and zero while I was taking damage, and this makes my health bar not work as intended as well.

Here’s the code:

public class PlayerHealth : MonoBehaviour
{
  
    [System.Serializable]
    public class PlayerStats
    {
      
        public int maxHealth;

        private int _curHealth;
        public int CurHealth
        {
         get { return _curHealth; }
         set { _curHealth = Mathf.Clamp(value, 0, maxHealth); }

        }
        public void Init()
        {
            CurHealth = maxHealth;
        }
    }

    public PlayerStats PStats = new PlayerStats();

  

    private void Awake()
    {
       //I tried many ways to set the health at the beggining, didn't work with void start as well
      
        PStats.Init();
    }
    public void DamagePlayer(int damage)
    {

        PStats.CurHealth -= damage;
       
        if (PStats.CurHealth <= 0)
        {
           //this works ^^^
            GameMaster.KillPlayer(this);
        }
      
    }
  
    void Start()
    {
        //PStats.CurHealth = 100;
    }

   
}

change this area of code to:

private int _curHealth;
       public int CurHealth
        {
         get { return _curHealth; }
         set { _curHealth = Mathf.Clamp(_curHealth - value, 0, maxHealth); }
        }

i think thats the only problem i see.

The character doesn’t die with this code :frowning:

actually i may have erred in my last post. ignore that. hmmm, here is my health script which is working for me. what happens if you try it?

using UnityEngine;
using UnityEngine.Events;

/// <summary>
/// Script to give a game object a health meter.
/// </summary>
public class TimsHealth : MonoBehaviour
{
    [Tooltip("If true, actor ignores damage.")]
    public bool invunerable;

    [Tooltip("The maximum health of this actor.")]
    public float maxHealth = 100f;

    public UnityEvent onTakeDamage;     // Unity event fired when taking damage.
    public UnityEvent onDeath;          // Unity event fired on death.


    /// <summary>
    /// Is this actor alive?
    /// </summary>
    public bool isAlive { get; internal set; }

    /// <summary>
    /// Hit points of the actor.
    /// </summary>
    public float HitPoints
    {
        get
        {
            return _hitPoints;
        }
        set
        {
            _hitPoints = value;
            _hitPoints = Mathf.Clamp(_hitPoints, 0f, maxHealth);

            if (_hitPoints < 1f)
            {
                _hitPoints = 0f;
                isAlive = false;

                onDeath.Invoke();
            }
        }
    }

    [Header("DEBUGGABLES:")]

    [SerializeField]
    private float _hitPoints;

    // Initialize component.
    private void Start()
    {
        isAlive = true;
        HitPoints = maxHealth;
    }



    /// <summary>
    /// Function to cause damage to this GameObject.
    /// </summary>
    public void TakeDamage(float damage, Vector3 hitPoint, Vector3 hitNormal, int damageTypeID = 0, int damagedMaterialID = 0)
    {
        if (isAlive == false || invunerable)
            return;

        onTakeDamage.Invoke();

        HitPoints -= damage;
    }
}