- Home /
Life Bar System Help
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");
}
Post the GUI script. I strong recommend you to use an Array for life variable.
I agree with FL 4. An array would make this code a lot more clean, and you could possibly match your array index with your liveamount for more simpilicity.
Answer by Sillan · Feb 13, 2013 at 12:56 AM
I don't really have a GUI script. I have one on the unlocked icon gui's which load a particular level. Do you know how to do an array variable? Thankyou so much for your help :D
Unity Script Reference - Arrays
There's plenty of documentation on it; You could even pick up a book on Javascript and learn a few neat things.
Your answer
Follow this Question
Related Questions
Damage not triggering in build 1 Answer
How To Make Health Decrease 4 Answers
Health Bar fire damage 1 Answer
Health Bar and PlyGame 0 Answers