• 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
0
Question by Juri · Oct 21, 2010 at 07:02 PM · healthbarhealth

health regeneration after death

hey, i have a problem getting this script to work right!

static var health : int = 340; private var hitPoints: int; private var backup = [health]; static var Hit : boolean;

function OnGUI () { GUI.Box(Rect(8,9,350,40), "Life"); GUI.Box(Rect(13,25,health,20),""); }

function Update () { hitPoints = Hunt.killPoints; if(Player_Simply.dead == false && Hit == true) { health -= hitPoints * Time.deltaTime; } if(health <=1){ Player_Simply.dead = true; health = backup[0]; } }

my basic problem is, that when ever the static var dead is true, my player gets respawn. normally this script should then set the health var back to its maxHealth, but the player is still losing health! can u guys help me?

Comment
Add comment · 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 skovacs1 · Oct 21, 2010 at 07:12 PM 0
Share

This looks substantially similar to http://answers.unity3d.com/questions/24184/health-system-problems but with code this time. Perhaps you should consider closing the older question.

1 Reply

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

Answer by skovacs1 · Oct 21, 2010 at 07:22 PM

There are two things to consider:

Health regeneration

You set health to backup[0] .... why are you storing an array for one int?...If backup[0] is not a valid number, your health will not be reset to a value you expect.

What it takes to get damaged

Assuming your health is getting reset, the next thing to consider is the condition under which you take damage:

Player_Simply.dead == false && Hit == true

  • (Ignoring the terrible name) Player_Simply, assuming this.dead actually indicates that your player is alive then this will be true every frame that your player is alive, meaning that they can take damage as long as they are alive.
  • The only thing needed to take damage when you are alive (assuming the above variable is set properly) is for the boolean Hit to be true.

If you keep taking damage when you don't intend to, it must be that at some point you should probably set Hit to false but haven't.

Comment
Add comment · Show 1 · 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 Juri · Oct 22, 2010 at 08:37 AM 0
Share

thanks u were right, i just needed to set it to false! and the name is terrible, but this is just a test game. I need to make a video about this game, how i made it and everything, so i have more then one player scripts, and this one is the simple one (with out any animations)

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

How Do I Make A Health Bar 6 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Health subtracts when hit by bullit 2 Answers

Regain health on GUI 2 Answers

Help w/variables 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