Health Script not working

I have this health thing that theres a icon and text, if the health is over 50% its a certain picture and below 50% is another picture and max and health also have their own pictures.
But if it goes down less than 50% the texture changes and if you go back up to over 50% the texture doesnt change back?? how do I fix this

Heres the code:

var curHealth : int = 100;
var mySkin : GUISkin;

var HealthDead : Texture2D;
var Health1 : Texture2D;
var Health2 : Texture2D;
var HealthMax : Texture2D;

function OnGUI(){
 	GUI.skin = mySkin;
    GUI.Box (Rect (10,10,92,116), curHealth.ToString() + "%");
    GUI.DrawTexture(Rect(20,40,72,72),HealthMax);
}
 
function Update () {
 	
	if(Input.GetKeyDown(KeyCode.DownArrow)) {
 		curHealth -= 10;
 	}
 	
 	if(Input.GetKeyDown(KeyCode.UpArrow)) {
 		curHealth += 10;
 	}
 	
 	//Health Icon
	if(curHealth < 0) {
		curHealth = 0;
	}
	
	if(curHealth > 100) {
 		curHealth = 100;
 	}
 	
 	if(curHealth >99) {
 		HealthMax = HealthMax;
 	}
 	
 	 if(curHealth <100 && curHealth >50) {
		HealthMax = Health2;
 	}
 	
 	if(curHealth <50 && curHealth >0) {
		HealthMax = Health1;
 	}
}

You’re changing HealthMax into all of the other different health images…

The solution is to use a new variable called HealthCurrent instead of applying it all onto HealthMax…

Here’s a working version of the code:

var curHealth : int = 100;
var mySkin : GUISkin;
 
var HealthDead : Texture2D;
var Health1 : Texture2D;
var Health2 : Texture2D;
var HealthMax : Texture2D;
var HealthCurrent: Texture2D = HealthMax;

function OnGUI(){
    GUI.skin = mySkin;
    GUI.Box (Rect (10,10,92,116), curHealth.ToString() + "%");
    GUI.DrawTexture(Rect(20,40,72,72),HealthCurrent);
}
 
function Update () {
 
    if(Input.GetKeyDown(KeyCode.DownArrow)) {
        curHealth -= 10;
    }
 
    if(Input.GetKeyDown(KeyCode.UpArrow)) {
        curHealth += 10;
    }
 
    //Health Icon
    if(curHealth < 0) {
        curHealth = 0;
    }
 
    if(curHealth > 100) {
        curHealth = 100;
    }
 
    if(curHealth >99) {
        HealthCurrent = HealthMax;
    }
 
     if(curHealth <100 && curHealth >50) {
        HealthCurrent = Health2;
    }
 
    if(curHealth <50 && curHealth >0) {
        HealthCurrent = Health1;
    }
}  

Hopefully that works. :wink: