- Home /
Why is my Player Health not set back to 100 when I exit game mode?
So I create my health as a scriptable object with the following code: (The health decreases automatically with time) [CreateAssetMenu] public class PlayerHealth : ScriptableObject {
public float health;
public float deathRate;
/*private void Update()
{
health = health - (deathRate * Time.deltaTime);
}
*/
} I then created the health scriptable object in my asset files, and set it to 100
And then I created UI slider to display my health on screen and created a script to manage UI: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class HealthUI : MonoBehaviour { public PlayerHealth player; public Slider HealthBar; // Start is called before the first frame update public PlayerHunger hunger; public Slider HungerBar;
public void Start()
{
player.health = 100f;
}
public void Update()
{
//HealthBar.value = HealthBar.maxValue;
player.health = player.health - (player.deathRate * Time.deltaTime);
HealthBar.value = player.health;
}
} When I exit game mode the slider goes back to full bar like it should however the health value in scriptable object did not-- in this case it stucks at 95 What am I doing wrong here? Where should the decreasing of health code be placed? In the UI or in the health scriptable object script????
Answer by logicandchaos · May 10, 2021 at 12:04 PM
because it a scriptable object, the value will remain when you enter and exit playmode. Just reset the value in OnAwake/OnStart/OnEnable of the player's gameObject.
Your answer
Follow this Question
Related Questions
unity health bar not losing health 1 Answer
How to change pixel inset of GUITexture? 2 Answers
How to make my health decrease? 1 Answer
Health Code, Damage and Healing 0 Answers
Self Reference and TextMesh inside of a script to change the text 0 Answers