- Home /
losing lives
I have made a code of when you enter the trigger of the enemy, you lose a heart. you have 3 hearts. when i get hit by the enemy, all my hearts go? WHY?
#pragma strict
var Lives : int = 3;
var LivesToDie : int = 0;
var Heart1 : GUITexture;
var Heart2 : GUITexture;
var Heart3 : GUITexture;
var DieScene : String;
function OnTriggerEnter(other: Collider){
Lives -= 1;
if(Lives >= 2){
Heart1.enabled = false;
}
if(Lives >= 1){
Heart2.enabled = false;
}
if(Lives >= 0){
Heart3.enabled = false;
yield WaitForSeconds(0.5);
Application.LoadLevel(DieScene);
}
}
PS: if its a really stupid error i'm only ten so thats probably why :/
Your code will work as written if you use less than ins$$anonymous$$d of greater than.
if(lives <=2){
However @robertbu solution is more elegant and will work better. It will allow you to gain hearts as well.
Answer by robertbu · May 09, 2014 at 07:21 PM
More power to you coding at 10. 'Lives' is correctly decremented. The issue is with your 'heart' and loading logic. I'm not sure which way you are counting your hearts, but try this instead:
function OnTriggerEnter(other: Collider) {
Lives -= 1;
Heart1.enabled = (Lives >= 1);
Heart2.enabled = (Lives >= 2);
Heart3.enabled = (Lives >= 3);
if (Lives <= 0) {
Application.LoadLevel(DieScene);
}
}
So.. now i have the full code :D
#pragma strict
var Lives : int = 10;
var LivesToDie : int = 0;
var Heart1 : GUITexture;
var Heart2 : GUITexture;
var Heart3 : GUITexture;
var DieScene : String;
function OnTriggerEnter(other: Collider) {
Lives -= 1;
Heart1.enabled = (Lives >= 1);
Heart2.enabled = (Lives >= 2);
Heart3.enabled = (Lives >= 3);
if (Lives <= 0) {
Application.LoadLevel(DieScene);
}
}
Your answer
Follow this Question
Related Questions
Checking Enemies In An Area. 1 Answer
why won't my code work? 1 Answer
how can i do a headshot with a rigidbody bullet? 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Enemy moving sideways 1 Answer