Im sure this is a novice question, so please bear with me. I am looking to change a “Healthbar” at runtime depending on the condition of the player. As a simple test case, I just want to show a bar with no “life” when the character is dead.
Ive tried a few different methods and am not able to change the existing full health bar to another sprite upon character death. This is what I got so have so far.
public class Player : MonoBehaviour
{
public int maxHealth = 3;
public int currentHealth = 3;
public int currentSpeed = 5;
public int currentScore = 0;
public Sprite Dead; // Assets/Sprites/Powerups/Health/No Health/Dead.png
public void OnDamage()
{
currentHealth--;
Debug.LogFormat("The player's current health is: {0}", currentHealth);
if (currentHealth == 0)
{
Dead = Resources.Load<Sprite>("Dead");
GameObject.Find("HealthBar").GetComponent<SpriteRenderer>().sprite = Dead;
OnDeath();
}
}
public void OnDeath()
{
Debug.Log("You're dead!");
Time.timeScale = 0;
}
}
I know this is probably a simple fix, but I can’t wrap my head around it just yet. Thanks for any help in advance.