- Home /
Life/Health Bar
Hey Guys, I'm new to unity and was looking for a bit of help. I wrote this script here to manage the Player's lives if hit by a bullet. When the player is hit, a value of 2 is added to the variable "liveamount". Then the script decides what gui texture to display depending on what value "liveamount" is. It works so far as when the player is hit "liveamount" goes up by 2 every time but further down where the script checks what number it is the gui textures don't change. Would anyone be able to help me. I would be so so greatful and give your credit in the game credits if i finish my game and put it on Google play. Thanks so much guys, Johnny.
This is the script:
var liveamount : int = 0;
var life1:GameObject;
var life2:GameObject;
var life3:GameObject;
var life4:GameObject;
var life5:GameObject;
var life6:GameObject;
var life7:GameObject;
var life8:GameObject;
var life9:GameObject;
var life10:GameObject;
var youlose:GameObject;
function OnTriggerEnter(hit : Collider){
if(hit.gameObject.tag =="helibullet")
{
Destroy(hit.gameObject);
liveamount +=1;
}
}
function Update(){
}
if(liveamount == 0){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = true;
youlose.active = false;
}
else if(liveamount == 2){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = true;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 4){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = true;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 6){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = true;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 8){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = true;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 10){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = true;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 12){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = true;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 14){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = true;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 16){
//what guis to show
life1.active = false;
life2.active = true;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 18){
//what guis to show
life1.active = true;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = false;
}
else if(liveamount == 20){
//what guis to show
life1.active = false;
life2.active = false;
life3.active = false;
life4.active = false;
life5.active = false;
life6.active = false;
life7.active = false;
life8.active = false;
life9.active = false;
life10.active = false;
youlose.active = true;
yield WaitForSeconds(5.0);
Application.LoadLevel("LevelSelect");
}
Your answer
Follow this Question
Related Questions
How to make a camera's background be another camera's veiw 1 Answer
Taking a hit 3 Answers
How to prevent guitexture from vertically shrinking? 0 Answers
How to display gui.texture from a different script 1 Answer
If health lower than 0 display gui 0 Answers