Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by GiantsDomain · Jan 09, 2015 at 04:28 PM · decreasehealth

How do you create decreasing health over time?

I'm making a game where you are a medic on a battlefield and you have to heal several injured soldiers that are meant to lose a health point every couple of seconds. I am not sure exactly how to go about setting up the health to decrease over time - in the code.

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 Owen-Reynolds · Jan 09, 2015 at 06:57 PM 0
Share

I'm assu$$anonymous$$g you know how to decrease health. Just look up how to do "things" over time.

Tatanan shows a typical continuous change. But "unity do something every x seconds" has lots of results.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Tatanan · Jan 09, 2015 at 04:37 PM

The easiest way is using Update and FixedUpdate methods of a MonoBehaviour and then decreasing your health var by substracting a value proporcional to Time.deltaTime. Your code will be something like:

 using UnityEngine;
 using System.Collections;
 
 public class HealthUpdate : MonoBehaviour
 {
     public float health = 100.0f;
     private const float coef = 0.2f;
     
     void Update ()
     {
         health -= coef * Time.deltaTime;
     }        
 }

Comment
Add comment · Show 3 · 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 Dheeraj_Singhani · Apr 26, 2018 at 12:03 PM 0
Share

Hello, I am very to Unity. So please excuse my mistakes.

I did exactly as you commented. Healthbar is decreasing in the inspector panel, But nothing happens to the actual Healthbar in the game.

I have attached the script to Healthbar but the changes aren't getting affected. Please help. @Tatanan

avatar image Hellium Dheeraj_Singhani · Apr 26, 2018 at 12:47 PM 0
Share

@Dheeraj_Singhani : This answer is 3 years old. Not sure you will get an answer from Tatanan.

It's normal your health bar does not change because the script given by Tatanan does not handle it (since OP did not mention he needed it).

Depending on how you manage your health bar (image? slider?), you will have to make some adjustment to the code. An easy and flexible way is to do as follow:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.Events;
 
     [System.Serializable]
     public class HealthEvent : UnityEvent<float> { };
 
  public class HealthUpdate : $$anonymous$$onoBehaviour
  {
      public float health = 100.0f;
      public HealthEvent healthEvent ;
      private const float coef = 0.2f;
      
      void Update ()
      {
          health -= coef * Time.deltaTime;
          if( healthEvent != null )
              healthEvent.Invoke( health ) ;
      }        
  }


Then, in your inspector, you will see a "block" similar to the OnClick block of a UI button. Just add the gameObject + function you want to update your UI.

avatar image sarfree17 · Nov 05, 2018 at 12:41 PM 0
Share

So how much of life will decrease in that code? is it per second or $$anonymous$$ute?

avatar image
0

Answer by unity_xgvElRcu9eitDw · Mar 29, 2019 at 02:15 PM

void Update() { if (hp > 0) { count += Time.deltaTime;

         while (count > 5)
         {
             hp = hp - 1;
             HP.text = hp.ToString();
             count = 0;
         }
     }
 }
Comment
Add comment · 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
0

Answer by unity_xgvElRcu9eitDw · Mar 29, 2019 at 01:06 AM

void Update() { if (hp > 0) { count += Time.deltaTime;

         while (count > 5)
         {
             hp = hp - 1;
             HP.text = hp.ToString();
             count = 0;
         }
     }
 }
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Health Bar issue 0 Answers

Tool Durability if Hit any Collider 1 Answer

Help me with health script 2 Answers

How to make health and damage script function correctly? 3 Answers

How can I have my bullet hit more than 1 enemy? 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