RPG Combat healthpoints starting as the maximum

Hi guys, I am having issues with 11.19 Damage Progression where my maximum health is being selected when starting the game. It works fine for my enemies just not my player.

Any help will be greatly appreciated.

This is my code.
using System;

using TMPro;

using UnityEngine;

namespace RPG.Attributes

{

public class HealthDisplay : MonoBehaviour

{

    [SerializeField] TextMeshProUGUI healthText;

    Health _health;

    private void Awake()

    {

        _health = GameObject.FindWithTag("Player").GetComponent<Health>();

    }

    private void Update()

    {

       GetComponent<TextMeshProUGUI>().text = String.Format("{0:0}/{1:0}", _health.GetHealthPoints(),

    _health.GetMaxHealthPoints());

    }

}

}

Hi Simon. What is the player heath showing? Is it 0, or null, or a wrong value? Can you post your health.cs and a screen shot of the player’s inspector?

Hi EDC, its showing the wrong value it should be showing 50 but it is showing the max of 250.

Health.cs
using System;

using System.Runtime.InteropServices.WindowsRuntime;

using UnityEngine;

using RPG.Saving;

using RPG.Stats;

using RPG.Core;

namespace RPG.Attributes

{

public class Health : MonoBehaviour, ISaveable

{

    [SerializeField] float regenerationPercentage = 70;

   

    float healthPoints = -1f;

    bool isDead = false;

    private void Start()

    {

        GetComponent<BaseStats>().onLevelUp += RegenerateHealth;

        if (healthPoints < 0)

        {

            healthPoints = GetComponent<BaseStats>().GetStat(Stat.Health);

        }

    }

    public bool IsDead()

    {

        return isDead;

    }

    public void TakeDamage(GameObject instigator, float damage)

    {

         print(gameObject.name + " took damage: " + damage);

        healthPoints = MathF.Max(healthPoints - damage, 0);

        if (healthPoints == 0)

        {

            Die();

            AwardExperience(instigator);

        }

    }

    public float GetHealthPoints()

    {

        return healthPoints;

    }

    public float GetMaxHealthPoints()

    {

        return GetComponent<BaseStats>().GetStat(Stat.Health);

    }

    public float GetPercentage()

    {

        return 100 * (healthPoints / GetComponent<BaseStats>().GetStat(Stat.Health));

    }

    private void Die()

    {

        if (isDead) return;

        isDead = true;

        GetComponent<Animator>().SetTrigger("die");

        GetComponent<ActionScheduler>().CancelCurrentAciton();

    }

    private void AwardExperience(GameObject instigator)

    {

        Experience experience = instigator.GetComponent<Experience>();

        if (experience == null) return;

        experience.GainExperience(GetComponent<BaseStats>().GetStat(Stat.ExperienceReward));

    }

    private void RegenerateHealth()

    {

        float regenhealthPoints = GetComponent<BaseStats>().GetStat(Stat.Health)

        * (regenerationPercentage /100);

        healthPoints = MathF.Max(healthPoints, regenhealthPoints);

    }

    public object CaptureState()

    {

        return healthPoints;

    }

    public void RestoreState(object state)

    {

        healthPoints = (float)state;

        if (healthPoints == 0)

        {

            Die();

        }

    }

}

}

Its ok found the issue I had my player starting level in the base stats in the inspector set to 5 :crazy_face:

Excellent. Glad you solved the problem.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms