- Home /
weird results on mobile
I have a script in which after 4 hours have passed, bonus level must be unlocked. everything works fine in Unity Editor but when I play the game on mobile, It doesn't work. What's wrong?
using UnityEngine;
using System.Collections;
using System;
using System.Globalization;
public class Date : MonoBehaviour
{
DateTime currentDate;
DateTime oldDate;
public static bool bonus;
void Start()
{
//Store the current time when it starts
currentDate = System.DateTime.Now;
var oldTimeString = PlayerPrefs.GetString("sysString", "10");
if(!string.IsNullOrWhiteSpace(oldTimeString))
{
//Grab the old time from the player prefs as a long
if(long.TryParse(oldTimeString, out var temp))
{
//Convert the old time from binary to a DataTime variable
var oldDate = DateTime.FromBinary(temp);
print("oldDate: " + oldDate);
var difference = currentDate.Subtract(oldDate);
if(difference.Hours>4)
{
bonus=true;
print ("Yes");
}
else
{
print ("No");
}
}
}
else
{
Debug.Log("First start -> No");
}
}
void OnApplicationQuit()
{
//Savee the current system time as a string in the player prefs class
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
print("Saving this date to prefs: " + System.DateTime.Now);
}
}
Answer by BartekWilk · Jul 05, 2020 at 04:08 PM
I think that OnApplicationQuit is not good to use on mobile. You can try using OnApplicationFocus instead of OnApplicationQuit - look at this link
sorry I'm new to Unity. I copied this code from StackOverFlow. As I'm learning C# for program$$anonymous$$g in unity, I guess this code isn't in C#. is that right? all of my project scripts are in C#. I tried both OnApplicationFocus() and OnApplicationPause() but none of them worked.@BartekWilk
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
KEEP INSTANTIATE PREFAB ONMOUSEDOWN 1 Answer