So I decide to create a struct to save health points and the death state of the enemies. But then weird behavior appear. I test by killing one enemy, then save, and killing another one and load the save game. What happens next is:
- 1st enemy still dead
- 2nd enemy, suppose to be still alive, now still alive but in dead posture, he still chasing me around while lying dead but cannot hit me
If I then unplay and play again, then:
- 1st enemy still dead, but stand still and doing nothing, he died in living posture
- 2nd enemy is alive and behaves normally as expected
It must be in the way that heathPoints, Die() and other methods and classes interact with each other. Is there anyway in Health class alone can signify a character should be dead (based on isDead and healthPoints) when save or scene reload
Here is my code where I implement the struct
[System.Serializable]
struct HealthSaveData
{
public float healthPoints;
public bool isDead;
}
public object CaptureState()
{
HealthSaveData data = new HealthSaveData();
data.healthPoints = healthPoints;
data.isDead = isDead;
return data;
//return healthPoints;
}
public void RestoreState(object state)
{
HealthSaveData data = (HealthSaveData)state;
healthPoints = data.healthPoints;
isDead = data.isDead;
//healthPoints = (float)state;
if (healthPoints == 0)
{
Die();
}
}