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

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.