- Home /
GUI health bar issue
Im having issues with GUI texture health bar, currently the player is receiving damage and when health reaches 0 the level restarts, but the GUI textures do not change. The health bar is represented by 3 different textures but it displays only one (full health) even when damage is being applied.
here is my code, can someone please help me with this one, thanks
static var health:int=4;
var health3: Texture2D;
var health2: Texture2D;
var health1: Texture2D;
function OnCollisionEnter(hit: Collision){
if(hit.gameObject.collider){
health = health - 1;
}
}
function Update () {
if(health == 0)
Application.LoadLevel("level01");
var health=GameObject.Find("health");
if(health==3){
guiTexture.texture = health3;
}
if(health==2){
guiTexture.texture = health2;
}
}
Answer by Berenger · Feb 18, 2012 at 08:57 PM
You don't need to check health value in Update, as it only changes in OnCollisionEnter. So check it after the line health = health - 1;
However your script should work already, so I guess the collision event isn't raised. Try adding some print(...) to see where the program goes.
Answer by 5ky1ine · Feb 19, 2012 at 01:15 AM
ok, i removed the update function and the following line var.health=GameObject.... it works as it did before, the textures don't change. what is a print ()? sorry I'm new to scripting, if you can give an example please i can copy it into my script. thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613064222im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GuiTexture above 3D objects 1 Answer
GUI healthbar 3 Answers
Detect if any GUI is being displayed on-screen? 1 Answer
percentage health bar 1 Answer
Health Code, Damage and Healing 0 Answers