FormatException: long to long??

Edit:
I think I found a solution…
Using, PlayerPrefs.HasKey()…

//check if this value exists in player prefs...
if(PlayerPrefs.HasKey("somKey")){
//OK!
}

//ORIGNAL
Hi,
At Start() I am gathering the last time, in real time the game was played, it works fine if, I have played before, however, if I clear out all PlayerPrefs, saved data, and essentially, start from scratch, I get an error.

//the error occurs at the last line, print("3. "…

DateTime date = DateTime.Now;
print("1." + date);
//convertto binary
long date_bin = date.ToBinary();
print("2." + date_bin);
//getthebinarybackfromthesave data
long old_date_bin = long.Parse(PlayerPrefs.GetString("date"));
print("3." + old_date_bin);

I am trying to figure out how I can if, first time playing (or info is null for lack of a better term) move over this.

Another option is to use the overload of PlayerPrefs methods that accept a default value.

string playerPrefsDate = PlayerPrefs.GetString("date", DateTime.Now.ToString);
long old_date_bin = long.Parse(playerPrefsDate);

Additionally, I highly, highly, highly recommend using TryParse() methods instead of Parse() whenever possible. Error handling when parsing in strings is a good habit to develop.

1 Like