Health Bar Messages

The healthbar works fine. I am not wondering if it is possible for an “image” to pop up next to the health bar? Assume Health will be 100. And there are 5 different images. One image when health is 100, another image when health is 80, another image for 60, another 40, etc.Using the UI because that’s how the healthbar is made. Any ideas?

public Image one;
public Image two;
public Image three;
public Image four;
public Image five;


	private void HandleHealth()
	{
		healthText.text = "Health";
		float currentXValue = MapValues (currentHealth, 0, maxHealth, minXValue, maxXValue);
		healthTransform.position = new Vector3 (currentXValue, cachedY);

		if (currentHealth > maxHealth / 2) {
			visualHealth.color = new Color32 ((byte)MapValues (currentHealth, maxHealth / 2, maxHealth, 255, 0), 255, 0, 255);
		}
		else 
		{
			visualHealth.color = new Color32 (255, (byte)MapValues (currentHealth, 0, maxHealth / 2, 0, 255), 0, 255);
		}
	}

normally I’d tell you that googling for a healthbar tutorial isn’t hard, but with the new UI there aren’t that many and a lot of them are overly complicated - so here is a good one:

this is a code i use it works for the most part and “healthimage” is just an image with filed check.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class enter : MonoBehaviour {

public Camera camera;
CharacterController cc;
public float health = 100.0f;
public Image healthimage;
public Text dead;
public BoxCollider spikes;

// Use this for initialization
void Start () {
	cc = GetComponent<CharacterController>();
	dead.enabled = false;
	healthimage.fillAmount = 100.0f;

}

// Update is called once per frame
void Update () {
	if (healthimage.fillAmount <= 0.0f) 
	{
		healthimage.fillAmount = 0.0f;
	};

	healthimage.fillAmount += 0.00053f * Time.deltaTime;
	health += 0.5f * Time.deltaTime;

	if(health >= 100)
	{
		health = 100;
	}

	if (health <= 0.1199999f)
	{
		health = 0;
		cc.enabled = false;
		audio.Stop ();
		dead.enabled = true;
		
	}

	if (healthimage.fillAmount <= 0)
	{healthimage.fillAmount = 0;
	}

OK you want to have an image and an array of sprites. For ease let’s just make the both public so you can just drag on the sprite in the inspector.

public Sprite[] hurtSprites;
public Image hurtImage;

Put an image on the Canvas and drag that onto the public hurtImage slot in the script.

Now we need to deal with the HandleHealth function:

void HandleHealth()
{
	if(health < maxHealth / 5)
		HealthIcon(0);
	else if(health >= maxHealth / 5 && health < maxHealth / 2.5f)
		HealthIcon (1);
	else if(health >= maxHealth / 2.5f && health < maxHealth / 1.6666f)
		HealthIcon (2);
	else if(health >= maxHealth / 1.6666f && health < maxHealth / 1.25f)
		HealthIcon (3);
	else if (health == maxHealth)
		HealthIcon (4);
}

so we set the 20%, 40% and 60% and so on with maths, that allows the max health to 100 or 320 or whatever. Note 60% is 1.66 recurring so never gonna be accurate but close enough for what we need.

void HealthIcon(int IconNumber)
{
	Debug.Log (hurtSprites.Length + " : " + IconNumber);
	hurtImage.sprite = hurtSprites[IconNumber];
}

And this last bit just sets the icon from the public array.