• 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
1
Question by QWERTY · Apr 08, 2010 at 12:31 AM · healthbar

Lives are running inconsistently.

Hi, In my game I have set up a lives system so that you have three lives and then you die. When I run the game the lives system will work most of the time. Other times the lives will not be removed or the game will play the game over scene before it is set to. Here is my script...

var health1 : Texture2D; //one live 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;
         break;

         case 2:
             guiTexture.texture = health2;
         break;

         case 1:
             guiTexture.texture = health1;
         break;

         case 0:
             Application.LoadLevel (2);
         break;
     }
 }

Any ideas on what is wrong.

Thanks

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Michael La Voie · Apr 08, 2010 at 12:35 AM

That code looks fine. My best guess is that the bug is in how the lives are decreased.

If you set the lives to decrease by 1 every time there is a collision, then a collision that lasts more than 1 frame could cause multiple lives to be lost at once. That's just an example. You'll need to elaborate more on how lives are decreased and post some code to figure this out.

Update

I saw you posted this code:

private var dead = false;
function OnControllerColliderHit(hit : ControllerColliderHit) { 
    if(hit.gameObject.tag == "Finish") { 
        dead = true; 
        //subtract life here 
        Health.LIVES -= 1; 
    }
}

You could set up an invulnerability period between collisions to prevent this bug:

private var dead = false; private var timeSinceLastCollision = 0;

function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "Finish" && timeSinceLastCollision <= 0) { dead = true; //subtract life here Health.LIVES -= 1; timeSinceLastCollision = 3;//Wait at least 3 seconds between collisions } timeSinceLastCollision -= Time.deltaTime; }

Comment
Add comment · Show 4 · 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 QWERTY · Apr 08, 2010 at 01:48 AM 0
Share

Thanks that fixed it. There is still the problem that sometimes the game over scene is played before all lives are lost.

avatar image Michael La Voie · Apr 08, 2010 at 03:50 AM 0
Share

Good to hear. If you liked it, vote it up and accept it. As for ending too soon, you may have to ask a second question for that. Have you tried using Debug.Log(Health.LIVES); in an Update statement? That could help you track down when the lives decrease too quickly.

avatar image QWERTY · Apr 09, 2010 at 01:58 AM 0
Share

It is working now. Thanks

avatar image Michael La Voie · Apr 09, 2010 at 03:36 AM 0
Share

No problem. If it helped you, please vote it up and accept it as the answer

avatar image
1

Answer by Peter G · Apr 08, 2010 at 12:38 AM

I'm not sure if you are running in the editor or building your game or what, but your LIVES are a static var and that means there is only one instance so if your player lost some lives then the level was loaded again, he would have the same number of lives as he did last time.

Or like Michael said, the error is more than likely in another part of your code that applies the loss of lives.

Comment
Add comment · 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 QWERTY · Apr 08, 2010 at 12:45 AM 0
Share

How could I do that. Right now this is what I have in another script: private var dead = false; function OnControllerColliderHit(hit : ControllerColliderHit) { if(hit.gameObject.tag == "Finish") { dead = true; //subtract life here Health.LIVES -= 1; } I am new to this and don't know much about scripting.

avatar image Peter G · Apr 09, 2010 at 10:08 PM 0
Share

Here's my preferred way. declare var health : Health; at the top of your script. Then in function Start () add this health = GetComponent(Health);. Then take out Health.LIVES -=1 and change it to health.LIVES -= 1;. In your health script remove the static keyword.

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

No one has followed this question yet.

Related Questions

health bar 2 Answers

How Do I Make A Health Bar 6 Answers

Problem with health bar 1 Answer

What is the end of the script underlined? 1 Answer

scrollbar 1 Answer


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