• 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 L1vD3v · Mar 22, 2014 at 11:40 PM · javascriptregenerateminutehealth

Regenerate health over time

I have this integer static var energyObj : int = 5; and then I have got another script, which subtracts one "energyObj" if you lose a level, then if you have zero "energyObj", you cant play before they have regenerated atleas one. Now my problem is, I cant seem to find a way to regenerate every 30 minute, i can only find ways to do it each second with the Time.deltatime var but thats not how it is supposed to regenerate. Is there a way to make it regenerate every 30 minute instead of each second?

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 robertbu · Mar 22, 2014 at 11:42 PM 0
Share

30 $$anonymous$$utes is just 1800 seconds. You can use Invoke(), InvokeRepeating(), coroutine, or a timestamp depending on your need. Post your code and someone can show you how to modify it for 1800 seconds.

2 Replies

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

Answer by 0tacun · Mar 22, 2014 at 11:46 PM

use a loop

 var counter : float;
 
 function Update(){
  counter += Time.deltaTime;
  if(counter > 30minutes){
   energyObj++;
   counter = 0.0; }
 
  //if energyObj should't be above 5
  if(energyObj > 5)
  energyObj = 5;
 }
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 L1vD3v · Mar 22, 2014 at 11:53 PM 2
Share

It works, Thank you very much for your respond.

avatar image
0

Answer by SkaredCreations · Mar 22, 2014 at 11:54 PM

You can use a coroutine with an infinite loop inside which you have a yield WaitForSeconds(1800 / maxEnergy) and increase energyObj by 1. You can call this coroutine in your Start function and it will be ever active, it depends on your game logic.

For example (may be there's few minor syntax oversight, I use C# usually):

 function Regen() {
    var maxEnergy : int = 5;
    var seconds : float = 1800 / maxEnergy; // 1800 is 30 minutes in seconds
    while (true) {
        if (energyObj < maxEnergy)
           energyObj++;
        yield WaitForSeconds(seconds);
     }
 }
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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 People are following this question.

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

Related Questions

how can i do a headshot with a rigidbody bullet? 1 Answer

Damage/Healing over time 1 Answer

Problem with PlayerHealth and renderer 1 Answer

Enemy health script using raycast not destroying tagged object 1 Answer

I need some health with my health/enemy script 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