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 /
avatar image
1
Question by fendorio · Apr 17, 2014 at 06:49 AM · timetimerdatetime

Resume timer after application exit

Hi there, nooby question lol.

I'm writing a timer that counts down from X minutes.

Countdown function:

 private void UpdateTime()
 {
     secondTillRefill -= Time.deltaTime;
     if (secondTillRefill <= 0)
     {
         currentLives++;
         secondTillRefill = interval.TotalSeconds;
     }
 }

Save the time the application exits - The game is for WP8 - does this work for that?

 private void OnApplicationQuit()
 {
     PlayerPrefs.SetString("TimeOnExit", DateTime.Now.ToShortTimeString());
 }

So when I resume I'm currently doing this in Awake:

  if (PlayerPrefs.HasKey("TimeOnExit"))
     { 
         var x = DateTime.Now - DateTime.Parse(PlayerPrefs.GetString("TimeOnExit"));
         secondTillRefill = x.TotalSeconds;
         PlayerPrefs.DeleteKey("TimeOnExit");
     }

And it doesn't work, hence the question xD.

So yeah say I exit the game and the timer is on 1:30 (1 minute, 30 seconds)

I restart the game 30 seconds later, the timer should show 1:00 (1 minute, 0 seconds).

Anyone point out the flaw?

Cheers

Comment
Add comment · Show 2
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 fendorio · Apr 17, 2014 at 07:18 AM 0
Share

Haha yeah you got it. Well the problem isn't solved. Seems OnApplicationQuit isn't actually being executed on the WP8 device, however that's now a different question. Convert your comment to an answer and I will +vote and accept :)

avatar image NoseKills · Apr 17, 2014 at 12:42 PM 0
Share

This might not be applicable if you have some special requirements but ... You could change the logic so you always just save the last time that a life was lost, and how many were left at that point. Then, whenever you start the application you could calculate :

1) seems I dropped down to 1 lives left 24 $$anonymous$$s ago.

2) recharging 1 life takes 5 $$anonymous$$s

3) now i should have 1 + (int)(24 / 5) = 5 lives and i have used (25 % 4) = 4 $$anonymous$$utes of the ongoing recharge period.

That way you wouldn't have to care about the status of the app/device.

1 Reply

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

Answer by NoseKills · Apr 17, 2014 at 07:01 AM

 var x = DateTime.Now - DateTime.Parse(PlayerPrefs.GetString("TimeOnExit"));

If you just look at the order of the subtraction, you notice that x gets bigger the longer you wait, right ? If I'm looking at it right, var x becomes is the amount of seconds you had the app closed, so you should subtract that from the saved value.

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 fendorio · Apr 17, 2014 at 12:43 PM 1
Share

Thank you! Late night/early mornings aren't my time to code it seems lol

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

21 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

Related Questions

How can I track how much time has passed since a game was turned off? 2 Answers

How to make a timer read to the .001 of a second 2 Answers

Check System Time and execute function 1 Answer

Minute Timer Issue 1 Answer

Timer running down too quickly 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