Glad to hear my basestats is good! I have done something slightly different with health and so it’s most likely an issue there, here is my health.cs
using System;
using System.Collections.Generic;
using GameDevTV.Utils;
using RPG.Core;
using RPG.Saving;
using RPG.Stats;
using UnityEngine;
namespace RPG.Attributes
{
public class Health : MonoBehaviour, ISaveable
{
BaseStats baseStats;
bool isDead = false;
public bool IsDead() { return isDead; }
LazyValue<float> maxHealth;
float currentHealth;
void Awake()
{
baseStats = GetComponent<BaseStats>();
maxHealth = new LazyValue<float>(GetInitialHealth);
currentHealth = maxHealth.value;
}
float GetInitialHealth()
{
return baseStats.GetModifiedStat(Stat.Health);
}
void Start()
{
maxHealth.ForceInit();
}
void OnEnable()
{
baseStats.onLevelUp += GetUpdatedHealth;
}
void OnDisable()
{
baseStats.onLevelUp -= GetUpdatedHealth;
}
public void TakeDamage(GameObject instigator, float damage)
{
print (gameObject.name + " took damage " + damage);
currentHealth = Mathf.Max(currentHealth - damage, 0);
if (currentHealth == 0)
{
Death();
AwardExperience(instigator);
}
}
void Death()
{
if (isDead) { return; }
if (this.gameObject.tag != "Player")
{
transform.GetChild(1).GetComponent<ParticleSystem>().Stop();
}
isDead = true;
GetComponent<Animator>().SetBool("die", true);
GetComponent<ActionScheduler>().CancelCurrentAction();
}
void AwardExperience(GameObject instigator)
{
Experience experience = instigator.GetComponent<Experience>();
if (experience == null) return;
float expReward = baseStats.GetModifiedStat(Stat.ExperienceRewards);
experience.GainExperience(expReward);
}
void GetUpdatedHealth()
{
// work out difference between new max hp and current max hp, then set max hp to new cap and heal the difference - this is different to course method
float newMaxHealth = baseStats.GetModifiedStat(Stat.Health);
float levelUpHeal = newMaxHealth - maxHealth.value;
maxHealth.value = newMaxHealth;
currentHealth += levelUpHeal;
}
public float GetHealthPercentage()
{
return 100 * (currentHealth / maxHealth.value);
}
public float GetCurrentHealth()
{
return currentHealth;
}
public float GetMaximumHealth()
{
return maxHealth.value;
}
public object CaptureState()
{
Dictionary<string, float> healthValues = new Dictionary<string, float>();
healthValues.Add("currenthealth", currentHealth);
healthValues.Add("maxhealth", maxHealth.value);
return healthValues;
}
public void RestoreState(object state)
{
Dictionary<string, float> storedHealthValues = (Dictionary<string, float>)state;
currentHealth = storedHealthValues["currenthealth"];
maxHealth.value = storedHealthValues["maxhealth"];
if (currentHealth <= 0)
{
Death();
}
}
}
}