x


Healthbar not always working, someone please help!

Hi im quite new here. I have a health script applied to a gui texture. It displays a health bar and cycles through the various stages of health. It should work but only seems to work half the time. the script is as follows:

var plane : GameObject;

var detonator1 : GameObject;

var mySound : AudioSource;

var damage0 : Texture2D;

var damage1 : Texture2D;

var damage2 : Texture2D;

var damage3 : Texture2D;

var damage4 : Texture2D;

var damage5 : Texture2D;

var damage6 : Texture2D;

var damage7 : Texture2D;

var damage8 : Texture2D;

var damage9 : Texture2D;

var damage10 : Texture2D;

static var LIVES = 11 ;

function Update () { switch(LIVES) {

    case 11:
       guiTexture.texture = damage10;       
    break;

    case 10:
       guiTexture.texture = damage9;
    break;

    case 9:
       guiTexture.texture = damage8;
    break;

    case 8:
       guiTexture.texture = damage7;
    break;

    case 7:
       guiTexture.texture = damage6;     
    break;

    case 6:
       guiTexture.texture = damage5;
    break;

    case 5:
       guiTexture.texture = damage4;
    break;

    case 4:
       guiTexture.texture = damage3;     
    break;

    case 3:
       guiTexture.texture = damage2;
    break;

    case 2:
       guiTexture.texture = damage1;
    break;
    case 1: 
       guiTexture.texture = damage0;

    detonator1.GetComponent("Detonator").Explode();
         mySound.Play();
         groundtrigger.triggered=1;
         crashforce= maincontrols.speed*10000;
         maincontrols.speed=0;
         gameover=1;
         plane.rigidbody.useGravity = true;
         //FadeAudio(fadeTime, Fade.Out);
         Camera.main.SendMessage("fadeOut");

}

}

The health bar works most of the time, but seems to get randomly stuck. One time it will switch through all cases and the plane will crash, then the next time it will appear to get stuck on ,for example, "case 3" and will not switch to the lower cases. The switch is made when my player collides with an enemy.

unction OnTriggerEnter(collision : Collider) { if(collision.gameObject.tag == "snake") {

    HealthControl.LIVES -= 1;

}

Most times when it gets stuck, it gets stuck at "case 2". It should work everytime but only works about 50% of the time, not sure if this is a problem with my script or a problem with unity. Any help would be greatly appreciated. I have tried a number of things and nothing seems to get it working.

more ▼

asked May 20 '12 at 10:55 PM

albosss12 gravatar image

albosss12
0 1 1

can u show how the health bar looks, ma b i can give u a better solution.

May 23 '12 at 12:12 PM flamy
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

nah does the same as when its case1. I had it as case0; originally.

more ▼

answered May 21 '12 at 11:29 AM

albosss12 gravatar image

albosss12
0 1 1

(comments are locked)
10|3000 characters needed characters left

Dunno if this is the cause for sure or not, but you don't seem to have a break on Case 0; is one necessary?

more ▼

answered May 20 '12 at 11:15 PM

Christopher K. gravatar image

Christopher K.
23 6 9 16

No luck, I had it as case0; originally, doesn't seem to make a difference.

May 21 '12 at 11:21 AM albosss12

I have just realised I read your answer wrong lol. I thing it should have one, but that didn't fix the problem. I realised It was skipping multiple cases at a time, and going past case1;, to say case-3; which didn't exist. So it had appeared that the case had not changed, but actually it had gone past the case.

May 23 '12 at 12:06 PM albosss12
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3668
x383
x236
x92

asked: May 20 '12 at 10:55 PM

Seen: 447 times

Last Updated: May 23 '12 at 12:12 PM