Help with Zelda type health bar!
When I run this code all hearts in my array change but I only want to change one. Where am I going wrong? Im a newbie and any help would be appreciated!!
(example when health is 2.75 all hearts show the heartthreequarters sprite)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class ui_health_bar : MonoBehaviour {
public character_healt_model healthmodel;
public Image[] heartimages;
public Sprite heart_empty;
public Sprite heart_full;
public Sprite heart_quarter;
public Sprite heart_half;
public Sprite heart_threequarter;
void Start(){
}
void Update () {
HealthbarHeartAmount ();
}
void HealthbarHeartAmount(){
for (int i = 0; i <= healthmodel.GetMaxHealth (); i++) {
if (healthmodel.starthealth <= i) {
heartimages [i].enabled = false;
} else {
heartimages [i].enabled = true;
SetHeartSprite (heartimages [i]);
Debug.Log ("enable heart: " + heartimages [i].name);
}
}
}
public void SetHeartSprite(Image image){
Debug.Log ("start");
double health = healthmodel.GetHealth ();
int rest = (int)healthmodel.GetHeartRest ();
// If i have health of ex 1.5 it returns 2.
Debug.Log (healthmodel.GetHealth () + " " + healthmodel.GetHeartRest ());
if (rest == 0) {
if (health <= 0) { image.sprite = heart_empty;
} else {
image.sprite = heart_full;
Debug.Log ("set heartfull");
return;
}
} else if (rest == 1) {
image.sprite = heart_quarter;
Debug.Log ("set heartquarter");
return;
} else if (rest == 2) {
image.sprite = heart_half;
Debug.Log ("set heartquarter");
return;
} else if (rest == 3) {
image.sprite = heart_threequarter;
Debug.Log ("set heartthreequarter");
return;
} else {
image.sprite = null;
return;
}
}
}
Your answer
Follow this Question
Related Questions
How to dynamically place images/sprites 0 Answers
Questions about building a simple 2d image carousel 0 Answers
I am trying to make my sprite show up as an image from a card database. 1 Answer
Shader Graph - Inherit from Sprite Renderer or Image Component 0 Answers
Decreasing PNG image size result increasing in edges artifacts 0 Answers