Health Bar (help please)

Hi…

I’m trying to change the text “health” in this OnGui function,to a health bar texture

 public Texture2D emptyTex;
public Texture2D fullTex;



void OnGUI(){
		//HEALTH BAR 
		Health php=(Health)GetComponent("Health");
		if(php){
		float hpp=php.CurrentHealth;
		GUI.Button(new Rect(250, 50, 300, 26), "health : "+hpp);
		}

Any help ,please. ???

Here are a couple of examples, you should be able to bodge something together from them:

simple-player-health-bar

And

Stupidly Simple Unity Healthbar

Health bars are pretty simple to understand once you understand the math behind it.

You have a max health, current health, and a max rect size the health bar can be.

So you just divide the current health with the max health and then multiply that by what the max width/height of the rect can be and you got your health bar.

float currentHealth = 90;
float maxHealth = 100;
Rect healthRect = new Rect(10, 10, 100, 30);

void Update()
{
healthRect = new Rect(10, 10, (currentHealth/maxHealth) * 100, 30);
}

Thanks for your answers

well … my problem is not display the bar (with Gui.Drawtexture)… its interacting with other elements of the game (how bar reduce with the damage of the enemy ???)

(I’ll put the parts of code that use for it)

1-/ AI SCRIPT

if(Combat){
Health hp=(Health)target.transform.GetComponent("Health");
(hp.CurrentHealth>0){
if(hp){
hp.CurrentHealth=hp.CurrentHealth-Damage;

PLAYERCONTROLLER

public Texture BarTexture;       //bar texture

   void OnGUI(){
    
      Health php=(Health)GetComponent("Health");
      if(php){
      float hpp=php.CurrentHealth;
     GUI.DrawTexture(new Rect(250, 80,hpp, 35),BarTexture);

*this is the script where are the function OnGui

and finally the health script (shared by player/enemy in the game) and that is the one that have the variables and CurrentHealth and MaxHealth

using UnityEngine;
using System.Collections;

public class Health : MonoBehaviour {
	public float MaxHealth=100;
	public float CurrentHealth;
	public bool Invincible;
	public bool Dead;
	
	// Use this for initialization
	void Start () {
		//MAKE THE CURRENT HEALTH THE MAX HEALTH AT START
	CurrentHealth=MaxHealth;
	}
	
	// Update is called once per frame
	void Update () {
	
		//IF INVINCIBLE, HE CANNOT DIE..
		if(Invincible){
		CurrentHealth=MaxHealth;	
		}
		else{
		if(CurrentHealth<=0){
			CurrentHealth=0;
			Dead=true;
		}	
			
		//MAX HEALTH
			if(CurrentHealth>=MaxHealth)CurrentHealth=MaxHealth;
			
			//WHEN DEATH IS UPON HIM
		if(Dead){
				//TELL THE AI SCRIPT HE IS DEAD
			FreeAI AI=(FreeAI)GetComponent("FreeAI");
				if(AI){
			if(AI.IsDead){
			}
			 
			else AI.IsDead=true;
			 
		}
		}
		}
	
	}
}

How can reduce the health bar in the Playercontroller script ???

Thanks in advance