Hello, Whenever I save my game and play again the health is always full rather than what it should be from previous health. For Example; My player has a health of 60% in a current game and if i have to gain play the health is full. How to solve this ?
here is the code from the lecture , I even don’t know where is the error,
using UnityEngine;
using RPG.Saving;
using RPG.Stats;
using RPG.Core;
using System;
namespace RPG.Resources
{
public class Health : MonoBehaviour, ISaveable
{
float healthPoints = -1f;
bool isDead = false;
private void Awake()
{
if (healthPoints < 0)
{
healthPoints = GetComponent<BaseStats>().GetStat(Stat.Health);
}
}
public bool IsDead()
{
return isDead;
}
public void TakeDamage(GameObject instigator, float damage)
{
healthPoints = Mathf.Max(healthPoints - damage, 0);
if(healthPoints == 0)
{
Die();
AwardExperience(instigator);
}
}
public float GetPercentage()
{
return 100 * (healthPoints / GetComponent<BaseStats>().GetStat(Stat.Health));
}
public float GetHealthPoints()
{
return healthPoints; //Or whatever you named your varable for current heatlh points.
}
private void Die()
{
if (isDead) return;
isDead = true;
GetComponent<Animator>().SetTrigger("die");
GetComponent<ActionScheduler>().CancelCurrentAction();
}
private void AwardExperience(GameObject instigator)
{
Experience experience = instigator.GetComponent<Experience>();
if (experience == null) return;
experience.GainExperience(GetComponent<BaseStats>().GetStat(Stat.ExperienceReward));
}
public object CaptureState()
{
return healthPoints;
}
public void RestoreState(object state)
{
healthPoints = (float) state;
if (healthPoints <= 0)
{
Die();
}
}
}
}