Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by Developer1212 · May 30, 2017 at 02:12 PM · timedatetimeenergy

Energy refill script bug fix

Hi guys. I am making an energy refill script for my game right now. What it does is it detects whenever the player's energy is below 20 (the maximum amount) and then counts down from 30 seconds. (I've programmed all of the not-in-game stuff and in the real game it's obviously going to take longer than 30 seconds to refill, I've made it 30 seconds for test purposes) However, I find that this doesn't work sometimes and I think i know why. I use DateTime.Now.Second for this a lot and if I exit my game in the second half of a minute, by the time 30 seconds has passed it will already be the beginning of the next minute and the seconds will be lower and therefore will not meet my condition of if (PlayerPrefs.GetInt ("ExitTime") + 30 < System.DateTime.Now.Second) { Reference.energy += 2; }

What property should I use to get the time in seconds which doesn't restart to 0 every minute? 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
0

Answer by FM-Productions · May 30, 2017 at 02:50 PM

I would not use the System.DateTime methods for those things. Unity has a good solution for time tracking, located in the Time class: https://docs.unity3d.com/ScriptReference/Time.html

You can use Time.time for scaled time since the start of the game (it is influenced by the timeScale) or Time.unscaledTime if you want to use realtme values.

My implementation may not be the best, but it should give you a general idea. Instead of MaxValue you could introduce a bool variable that checks if a refill is pending or not. And only set the refillStartTime if no refill is already pending:

 float refillStartTime = float.MaxValue;
 
 private void refill(){
     if (refillStartTime == float.MaxValue && energy < 20){
         refillStartTime = Time.unscaledTime;
         //sets the start time to a specific value below the max float value, but only if the start time has not been set before. 
     }
     
     if (refillStartTime < float.MaxValue && refillStartTime + 30 > Time.unscaledTime){
         //only evaluated when the start time is below the maxValue and is reset after the energy increase was applied
         energy += 2;
         refillStartTime = float.MaxValue;
     }
 
 }

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 Zooow · May 30, 2017 at 03:44 PM

You could use TimeSpan.TotalSeconds, but you got to code it a bit differently :

  1. save the moment when you start to count time for refill (in savegame, so if player kill your application and come back, it's still valid)

  2. use TimeSpan to mesure elapsed time, and refill your energy bar adequatly.

in code, it would be something like :

         // energy is below 20, so time to refill and you're not already refilling
         DateTime startTimeToRefill;
         if(startTimeToRefill != null)
             startTimeToRefill = DateTime.Now;
 
         //... save startTimeToRefill in your savegame...
 
         // mesure time elapsed and find out how much energy has been refilled
         TimeSpan elapsedTime = DateTime.Now - startTimeToRefill;
         int energyGained = elapsedTime.TotalSeconds % 30;
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

67 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 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 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

Energy refill script bug fix 0 Answers

How to make notification appear at certain time without having to press button again? 2 Answers

Why doesn't this energy refill code work when I'm not on the game? 2 Answers

Check How Many Minutes Have Passed 1 Answer

How to Work Out Time Passed in Seconds (Using System.DateTime.Now)? 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