Quantcast
Channel: Questions in topic: "playerprefs"
Viewing all articles
Browse latest Browse all 1333

Formatting Exception when using DateTime for PlayerPrefs

$
0
0
Hi i need help, i am getting a FormatException when i try the below codes, it will save datetime and save to playerprefs then on resume will get the difference from the old time and current time, but i am having an Format exception, anyone who knows how to convert these things without errors? thank you import UnityEngine; import System.Collections; import System; var currentDate : DateTime; var oldDate : DateTime; var timex : double; function Start() { //Store the current time when it starts currentDate = System.DateTime.Now; //Grab the old time from the playerprefs as a long var temp : long = Convert.ToInt64(PlayerPrefs.GetString("Time_Data")); //Convert the old time from binary to a DataTime variable var oldDate : DateTime = DateTime.FromBinary(temp); print("oldDate: " + oldDate); //Use the Subtract method and store the result as a timespan variable var difference : TimeSpan = currentDate.Subtract(oldDate); print("Difference: " + difference.TotalSeconds); timex = timex + difference.TotalSeconds; } function OnApplicationQuit() { //Save the current system time as a string in the playerprefs class PlayerPrefs.SetString("Time_Data", System.DateTime.Now.ToBinary().ToString()); print("Saving this date to prefs: " + System.DateTime.Now); }

Viewing all articles
Browse latest Browse all 1333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>