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
2
Question by arizvi · Jul 15, 2014 at 02:10 PM · timepausing

Time.realTimeSinceStartup behaves weirdly when sending app to background or locking the screen.

From the Unity script reference: realtimeSinceStartup also keeps increasing while the player is paused (in the background).

I put the application to background and it gave the correct amount of time since the app started for smaller durations of backgrounding (less than 3 mins). But it started giving wrong values for longer durations of backgrounding (5-10 mins). Is this a bug in Unity? Has anyone else faced the same issue?

BTW, this happens across devices. Tested on Android 4.1 and iOS 7.0

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

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by Bunny83 · Jul 16, 2014 at 02:56 AM

I made some tests and yes in some cases the value is smaller than it should be or than it was already. It's actually a strange bug since like the docs state it's based on the system time. If the value is messed up that would mean the start-offset got messed up somehow.

Anyways, you can use a script like this one. It does the exact same thing but doesn't have this strange bug:

 // C#
 // TimeHelper.cs
 using UnityEngine;
 using System.Collections;
 
 public class TimeHelper : MonoBehaviour
 {
     private static System.DateTime m_StartTime;
     statis TimeHelper()
     {
         m_StartTime = System.DateTime.Now;
     }
     public static float realtimeSinceStartup
     {
         get 
         {
             var timeSpan = System.DateTime.Now.Subtract(m_StartTime);
             return (float)timeSpan.TotalSeconds;
         }
     }
 }

Simply attach the script to a gameobject in your first scene. That will force the static constructor to initialize the offset variable. Keep in mind if the device needs the memory your app might get wiped out and if you "switch back" it's actually restarted. However you will notice that since the splash screen will be visible again.

To get the correct time, just replace your Time.realtimeSinceStartup with TimeHelper.realtimeSinceStartup.

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 arizvi · Jul 16, 2014 at 05:59 AM 2
Share

Hey, thanks for the prompt reply. Unfortunately, the snippet of code that you sent won't be very helpful because the end user can change the system time and that would affect System.DateTime.Now.

To counter this issue, we were pinging the server to get the serverTime, but that happened only once during app launch and we were relying on realtimeSinceStartup. Now that that idea is kind of failing, we have decided to get the updated time from the server when the app comes back to foreground.

avatar image Bunny83 · Jul 16, 2014 at 10:11 AM 0
Share

It you need this to prevent cheating, then yes, you should use an independent source for your time. $$anonymous$$ake sure you secure the WWW request at least with a hash / checksum ;). Url redirection and packet tempering isn't that hard on Android ;)

Time.realtimeSinceStartup wouldn't have worked anyways since, as you can read in the docs, it's also based on the system time.

avatar image arizvi · Jul 16, 2014 at 11:09 AM 0
Share

Well, from the multiple tests that I have conducted, I figured out that realtimeSinceStartup is independent of device time (time returned by the system timer is not equal to the device time itself). So it can obviously be trusted to get delta time, in a case where the app remains in the foreground at all times.

To fix the background issue, you would need to ping a server and get the latest serverTime.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Setting timeScale to 0 causing editor to freeze 0 Answers

How do I use Time.unscaledTime? 4 Answers

Time Function in Javascript? 1 Answer

increase over time 1 Answer

Time function in OnTriggerStay 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