if... help

Hello all.
I’ve been trying to figure this out but so far no luck.
In my script when i lose a life i got a text which goes from 3 to 0.
Now i want to add hearts instead of numbers
So far the numbers are working
So the script that work with the numbers is…

function OnTriggerEnter (collider:Collider) {
if(enable){
Destroy(collider.transform.gameObject); //destroy collided object

if(livesLeft > 0)
{
livesLeft --;
lives.text = “” + livesLeft;

}

Now i tried adding the hearts which i thought it would work but when i lose a life it goes from 3 hearts to 1 and i only got 2 lives now instead of 3. i tried doing different things but so far no luck.
Thanks all.

if(livesLeft >= 3)
{
livesLeft --;
lives.text = “” + livesLeft;

}
if(livesLeft >= 2)
{
livesLeft --;
lives.text = “” + livesLeft;
Hearts3.active = false;
Hearts2.active = true;
Hearts1.active = false;
Hearts0.active = false;

}
if(livesLeft >= 1)
{
livesLeft --;
lives.text = “” + livesLeft;
Hearts3.active = false;
Hearts2.active = false;
Hearts1.active = true;
Hearts0.active = false;
}

if(livesLeft >= 1) will always run. instead of >= just use =
also you should probably use arrays or something instead of doing this manually.

@Shamtastik Please use code tags when pasting code…it makes it a lot easier to read (The button to the left of the floppy disc). It may also be easier to use a switch statement than multiple if statements.

1 Like

You are doing livesLeft-- in each if. This means it executes multiples times. Bad idea.

Thank you all for the reply.