At the moment I have a health system in which the player’s health is measured out of 100 and stored as a variable (SaveSystem.CurrentPlayer.health).
I would like this to be displayed to the player by 10 onscreen hearts. My idea to achieve this is to have a “HeartIndex” variable that stores an integer telling the program which heart is currently the furthest one that is full. Each heart goes through 10 different graphics before it is emptied and goes to an empty heart (i.e. when health reaches 90, the 10th heart is now empty and the HeartIndex now corresponds to the 9th heart).
Now, my “start” code, which merely empties the hearts that should be empty before setting the HeartIndex value, works fine. From there, the update code is supposed to deal with “in-between” values, and it technically does work when the game first starts as starting the game with a health value of, let’s say 95 for an example, works just fine. However, once the game is running the hearts won’t deplete any further then they already have, even if health is depleted. Have used Debug.Log to check that the health is definitely depleting so I know that’s not the problem.
The reason I add 1 to HeartIndex in the update code is because HeartIndex starts counting at 0, which won’t work when multiplying it by 10 to compare to health.
Any help would be appreciated as I really can’t figure it out. Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HealthManager : MonoBehaviour
{
public Sprite HeartFill0;
public Sprite HeartFill1;
public Sprite HeartFill2;
public Sprite HeartFill3;
public Sprite HeartFill4;
public Sprite HeartFill5;
public Sprite HeartFill6;
public Sprite HeartFill7;
public Sprite HeartFill8;
public Sprite HeartFill9;
public Sprite HeartFill10;
public SpriteRenderer Heart1;
public SpriteRenderer Heart2;
public SpriteRenderer Heart3;
public SpriteRenderer Heart4;
public SpriteRenderer Heart5;
public SpriteRenderer Heart6;
public SpriteRenderer Heart7;
public SpriteRenderer Heart8;
public SpriteRenderer Heart9;
public SpriteRenderer Heart10;
public SpriteRenderer[] Hearts;
public int HeartIndex;
// Start is called before the first frame update
void Start()
{
Hearts = new SpriteRenderer[10];
Hearts[0] = Heart1;
Hearts[1] = Heart2;
Hearts[2] = Heart3;
Hearts[3] = Heart4;
Hearts[4] = Heart5;
Hearts[5] = Heart6;
Hearts[6] = Heart7;
Hearts[7] = Heart8;
Hearts[8] = Heart9;
Hearts[9] = Heart10;
if (SaveSystem.CurrentPlayer.health > 90)
{
HeartIndex = 9;
}
else if (SaveSystem.CurrentPlayer.health <= 90 && SaveSystem.CurrentPlayer.health > 80)
{
HeartIndex = 8;
Hearts[9].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 80 && SaveSystem.CurrentPlayer.health > 70)
{
HeartIndex = 7;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 70 && SaveSystem.CurrentPlayer.health > 60)
{
HeartIndex = 6;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
Hearts[7].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 60 && SaveSystem.CurrentPlayer.health > 50)
{
HeartIndex = 5;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
Hearts[7].sprite = HeartFill0;
Hearts[6].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 50 && SaveSystem.CurrentPlayer.health > 40)
{
HeartIndex = 4;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
Hearts[7].sprite = HeartFill0;
Hearts[6].sprite = HeartFill0;
Hearts[5].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 40 && SaveSystem.CurrentPlayer.health > 30)
{
HeartIndex = 3;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
Hearts[7].sprite = HeartFill0;
Hearts[6].sprite = HeartFill0;
Hearts[5].sprite = HeartFill0;
Hearts[4].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 30 && SaveSystem.CurrentPlayer.health > 20)
{
HeartIndex = 2;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
Hearts[7].sprite = HeartFill0;
Hearts[6].sprite = HeartFill0;
Hearts[5].sprite = HeartFill0;
Hearts[4].sprite = HeartFill0;
Hearts[3].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 20 && SaveSystem.CurrentPlayer.health > 10)
{
HeartIndex = 1;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
Hearts[7].sprite = HeartFill0;
Hearts[6].sprite = HeartFill0;
Hearts[5].sprite = HeartFill0;
Hearts[4].sprite = HeartFill0;
Hearts[3].sprite = HeartFill0;
Hearts[2].sprite = HeartFill0;
}
else if (SaveSystem.CurrentPlayer.health <= 10)
{
HeartIndex = 0;
Hearts[9].sprite = HeartFill0;
Hearts[8].sprite = HeartFill0;
Hearts[7].sprite = HeartFill0;
Hearts[6].sprite = HeartFill0;
Hearts[5].sprite = HeartFill0;
Hearts[4].sprite = HeartFill0;
Hearts[3].sprite = HeartFill0;
Hearts[2].sprite = HeartFill0;
Hearts[1].sprite = HeartFill0;
}
}
// Update is called once per frame
void Update()
{
if (((HeartIndex + 1) * 10) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill10;
}
else if ((((HeartIndex + 1) * 10) - 1) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill9;
}
else if ((((HeartIndex + 1) * 10) - 2) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill8;
}
else if ((((HeartIndex + 1) * 10) - 3) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill7;
}
else if ((((HeartIndex + 1) * 10) - 4) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill6;
}
else if ((((HeartIndex + 1) * 10) - 5) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill5;
}
else if ((((HeartIndex + 1) * 10) - 6) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill4;
}
else if ((((HeartIndex + 1) * 10) - 7) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill3;
}
else if ((((HeartIndex + 1) * 10) - 8) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill2;
}
else if ((((HeartIndex + 1) * 10) - 9) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill1;
}
else if ((((HeartIndex + 1) * 10) - 10) == SaveSystem.CurrentPlayer.health)
{
Hearts[HeartIndex].sprite = HeartFill0;
HeartIndex -= 1;
}
}
}