• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by albosss12 · May 20, 2012 at 10:55 PM · guiswitchhealthbarhealth

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.

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flamy · May 23, 2012 at 12:12 PM 0
Share

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

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Christopher K. · May 20, 2012 at 11:15 PM

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?

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image albosss12 · May 21, 2012 at 11:21 AM 0
Share

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

avatar image albosss12 · May 23, 2012 at 12:06 PM 0
Share

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.

avatar image

Answer by albosss12 · May 21, 2012 at 11:29 AM

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

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help With Enemy Health bars 3 Answers

unity health bar not losing health 1 Answer

Issue with GUITexture width. 1 Answer

How to make a camera's background be another camera's veiw 1 Answer

How do I make a custom health bar? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges