float value carries over from runtime causing issues + it doesnt update in the inspector
Hi there, I have a float for my players health variable that seems to carry over each time I run the game. For example i set the public player health float to 100 in my script. When the player is attacked in game and the DamagePlayer(float damage) function is called and the damage is subtracted from the health. If i stop the game and run it again the inspector will say the player has 100 health yet when debugging the heath value starts off the same value when I last ended the runtime. This has led to many times running the game and the player starting off with -200 health for example and causing the game to constantly respawn the player everytime they are attacked. public class Player : MonoBehaviour {
public float Health = 100f;
public int fallBoundary = -20;
void Start()
{
Health = 100f;
}
private void Update()
{
if (transform.position.y <= fallBoundary)
{
DamagePlayer(Mathf.Infinity);
}
public void DamagePlayer(float damage)
{
Health -= damage;
if (Health <= 0)
{
Debug.Log("You dead");
GameMaster.KillPlayer(this.gameObject);
}
Debug.Log("player health is now" + Health);
}
}
Im not sure whats causing this. Could it be the way im calling the DamagePlayer function? or is it something to do with the health variable? Ive tried making it private, serializable to no luck. The float doesnt update in the inspector at all either. I just dont get it, I have pretty much the exact same script on my enemy gameobject controlling their health and damage function and it works exactly as i intended.