- Home /
Problem with HealthBar
Well im new here guys.
I want to tell you that I am making a game for the iphone and well I have some problems but the most important now is the health bar, here is the code:
// Player goes here:
var Warrior: GameObject;
//Here are the textures of the health
var Health1: Texture2D; var Health2: Texture2D; var Health3: Texture2D;
//Times Dead
var fall = 0;
//Some Code
function Update () {
// If the player falls, the healthbar will decrease
if(Warrior.transform.position.y < -2 )
{
gameObject.Find ("Health");
//Change the HealthBar
guiTexture.texture = Health2;
fall = 1; //Now the fall times increases to 1, untill here all is ok
} }
if(Warrior.transform.position.y < -2 && fall == 1){
gameObject.Find ("Health");
//Change the HealthBar
guiTexture.texture = Health3;
}
Does anybody see a problem there?
My code does not has errors but the bar only decreases to Health2 if I fall again it doesnt decrease to Health3.
Some help?
Answer by Ejlersen · Jan 13, 2011 at 09:00 PM
That code is a bit weird.
- Why the Finds? You're not using the "Health" thingy-ma-jig anyways.
- The last if-statement is outside the Update function.
- Putting the last if-statement inside the Update function will give you a health bar with Health3 when y < -2, because you will set fall = 1 and y < -2 will still be correct.
O$$anonymous$$, i putted the second if on the update function, it worked but on the first fall it decreases to the health 3 automatically. I want the bar to decrease in different falls but I dont know how to specify the second, third etc.. falls
Think about your own code like this: If y < -2, then set to Health2 and set fall to 1, later you check again if y < -2 and if fall is 1, then set to Health3. Both situations are true in the same cycle. This is bad, since you won't see Health2. Therefore you need to check when its below -2 and when its above. If it goes from greater than -2 to less then -2, then you have to change to Health2. Then reset the check when you are above -2 again. You can use a boolean for this.
Your answer
Follow this Question
Related Questions
progress bar 1 Answer
How to move 2D textures? 0 Answers
Changing Mesh of an object depending on Health Value. C# 2 Answers
How do i add a healthbar/actionbar gui 4 Answers