Adding a floating health bar

Hello guys,

I need a simple health bar to add it on top of each of my spawned soldiers in a strategy game. Further down I will copy my script that takes care of health:

using UnityEngine;
using System.Collections;


public class health : MonoBehaviour {
	//max health. you set this to what you want his health to be
	public int maxhealth=50;
	public int currenthealth;
	//cant die
	public bool invincible;
	public bool dead;
	
	
		
	// Use this for initialization
	void Start () {
		givereward=true;
		
		AI ai=(AI)GetComponent("AI");
		if(ai) ai.health=maxhealth;
		
	currenthealth=maxhealth;
	healthsave=maxhealth;
		
	}
	
	// Update is called once per frame
	void Update () {
	
		
		
		//tell ai when health is changed
		if(healthsave>currenthealth|healthsave<currenthealth){
			AI ai=(AI)GetComponent("AI");
			if(ai){
			ai.health=currenthealth;
			healthsave=currenthealth;
				
			}
		}
		
		//death 
		if(dead){
			AI ai=(AI)GetComponent("AI");
			    if (ai && !ai.dead){ // if ai.dead not set yet...
    ai.dead = true; // set it and destroy capsule
    Destroy(transform.Find("Capsule").gameObject);
    }
			//tell ai that he is dead
			if(ai)ai.dead=true;		
			Destroy (gameObject,30);
			
		}
		else {
			givereward=true;
		}
		
		if(currenthealth>=maxhealth) currenthealth=maxhealth;
		
		//if health is less than zero
		if(currenthealth<=0){
			//check if invincible if not death
			if(invincible)dead=false;
			else dead=true;
			currenthealth=0;
		}
		//regenerate
		if(regenerate){
			if(currenthealth<maxhealth&currenthealth>0){
		regtimer+=Time.deltaTime;
		if(regtimer>regenerationtime){
					currenthealth=currenthealth+regenerationamount;
					regtimer=0;
				}
		
		}
		}
	}
	

}

What is needed to add to the code to have a simple health bar ?
I tried and created one with GUIText, and eve though it was visible on the camera, it didn’t show when I attached it with the script to an object.

Please help :smiley:

Here you go! All you need to do is hook up your health variable.

using UnityEngine;

    public class HealthBar : MonoBehaviour 
    {
    	public Font font;
    	public int fontSize = 8;
    	public Vector3 Offset = Vector3.zero; // The offset from the character
    	
    	private float health = 100;
    	
    	private TextMesh bar;
    	
    	void Start()
    	{
    		// Setup the text mesh
    		bar = new GameObject("HealthBar").AddComponent("TextMesh") as TextMesh;
    		bar.gameObject.AddComponent("MeshRenderer");
    		bar.gameObject.transform.parent = transform;
    		bar.transform.localPosition = Vector3.zero + Offset;
    		
    		if(font) bar.font = font;
    		else bar.font = GUI.skin.font;
    		bar.renderer.material = font.material;
    		bar.characterSize = 0.25f;
    		bar.alignment = TextAlignment.Center;
    		bar.anchor = TextAnchor.MiddleCenter;
    		bar.fontSize = fontSize;
    	}
    	
    	void Update()
    	{
    		// Hook up your health variable here
    		if(bar.text != "HP: " + health.ToString()) bar.text = "HP: " + health.ToString();
    	}
    }

All I need is a script in C# that creates a healthbar (green front and grey Background), that decreases when health of object goes down.

I tried so many things for days now that is getting very frustrating considering I solved much more complicated things than this…

Any help would be gretely appreciated.