- Home /
(Health bar) Remove lives properly
Hey I tried to make a Health Bar, like the one in Tornado Twins video. (I also searched for other questions in here but no one matched my problem)
Anyway the thing is I got 3 lives and I want to loose 1 everytime I die like falling off the platform. I made some texture for the GUI, so 1 heart, 2 hearts, 3 hearts. But when I start I start at 2 lives (2 hearts) Instead of what should 3 and I dont loose any when I fall down to my "death" It looks like this
private var Dead = false;
function OnControllerColliderHit(hit: ControllerColliderHit)
{
if(hit.gameObject.tag == "fallout")
{
Dead = true;
//subtract life here
HealthControl.LIVES -= 1;
}
}
function LateUpdate()
{
if(Dead)
{
transform.position = Vector3 (0,4,0);
gameObject.Find("Main Camera").transform.position = Vector3(0,4,-10);
Dead = false;
}
}
@script RequireComponent(CharacterController)
This is in one of my scripts
var health1 : Texture2D; //one life left
var health2 : Texture2D; //two lives left
var health3 : Texture2D; //full health
static var LIVES = 3;
function Update ()
{
switch(LIVES)
{
case 3:
guiTexture.texture = health3;
case 2:
guiTexture.texture = health2;
break;
case 3:
guiTexture.texture = health1;
break;
case 0:
//gameover script here
break;
}
And this is in the other
And here is how I put up the heart pictures
Any ideas? cheers
Could is be that "fallout" should be "Fallout"
Concerning your switch you have twice case 3.
I fixed the case thing.
Now when I press play I start at 2 lives (2 hearts on screen) and I have to dive down to my death 2 times before it goes to 1 life (1 heart on screen)
Why doesnt it just go from 3 to 2 to 1 ?
Oh and the fallout thing has nothing to do with it, my "Fallout" object is tagget "fallout" so it does work :)
Answer by KellyThomas · Dec 08, 2013 at 02:53 PM
You are missing the break after your case 3
as a result execution falls through and runs the case 2
code as well.
Your code is currently:
case 3:
guiTexture.texture = health3;
case 2:
guiTexture.texture = health2;
break;
While it should be:
case 3:
guiTexture.texture = health3;
break;
case 2:
guiTexture.texture = health2;
break;
I just fixed the case 3 thing so its goes 3 to 2 to 1
Scenario now is i start on 2 lives (2 hearts on screen) and if I let my character fall down once nothing happens, twice it goes to 1 life (1 heart on screen)
Why don't I start at 3 and go from 3 to 2 to 1 like my cases says I should?
The fallout is with an lower case f because my Fallout object is tagged with a fallout (lower case f) tag
Please look again at the second block of code in my answer:
case 3:
guiTexture.texture = health3;
break; //this is the line you are missing
You will see that your code is missing the break for your case 3
.
As a result during execution when LIVES is equal to 3 you will assign the texture representing 3 lives, then immediately assign the texture representing 2 lives.
I didnt see you answer before, thanks alot buddy!
I am new here I have to get used to the diffrence of answer and comment.
Have a nice day!
Excellent, happy to help. Please remember to accept answers that satisfactorily resolve your questions. It marks them as resolved, and helps people navigate the site.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123704im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem vid GuiTexture 0 Answers
guitext is not changing color 1 Answer
Show GUITexture by picking an item. 0 Answers
Quality not working? 1 Answer
GUI.DrawTexture Artifacts 1 Answer