Code Below: is currently part of an void awake() where the if statement sets up all playerPrefs values for first time playing. The below else statement is trying to implement how many coins will be earned when the player re-opens the app.
How to Fix: How do I make it so it only runs this code when the application is re-Opened? Not every time the scene is reloaded.
Possible Improvements: If you see a easier way to do the below code to prevent time cheating, I am open to suggestions. I want only a max of 24Hr bonus coins or if open in less than that give equal value coins.
else
{
// Player Restart Game
// Check Time for coin bonus
ButtonCanvas.gameObject.SetActive(false);
OfflineCanvas.gameObject.SetActive(true);
// Same Year
if (PlayerPrefs.GetInt("year").Equals(System.DateTime.Now.Year))
{
// Same Month
if (PlayerPrefs.GetInt("month").Equals(System.DateTime.Now.Month))
{
// Same Day
if(PlayerPrefs.GetInt("day").Equals(System.DateTime.Now.Day))
{
// Add Coin Bouns for hours/min passed
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (((System.DateTime.Now.Hour - PlayerPrefs.GetInt("timeHour")) * 60) + (System.DateTime.Now.Minute - PlayerPrefs.GetInt("timeMin")));
}
else // Different Day
{
// Update if new day is higher than old
if (PlayerPrefs.GetInt("day") < System.DateTime.Now.Month)
{
PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
// Add Coin Bonus For Time 24 hrs
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
}
}
}
else // Different Month
{
// Update if new month is higher than old
if (PlayerPrefs.GetInt("month") < System.DateTime.Now.Month)
{
PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
// Add Coin Bonus For Time 24 hrs
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
}
}
}
else // Different Year
{
// Update if new year is higher than old
if (PlayerPrefs.GetInt("year") < System.DateTime.Now.Year){
PlayerPrefs.SetInt("year", System.DateTime.Now.Year);
PlayerPrefs.SetInt("month", System.DateTime.Now.Year);
PlayerPrefs.SetInt("day", System.DateTime.Now.Year);
PlayerPrefs.SetInt("timeHour", System.DateTime.Now.Hour);
PlayerPrefs.SetInt("timeMin", System.DateTime.Now.Minute);
// Add Coin Bonus For Time 24 hrs
offlineCoinCount = PlayerPrefs.GetInt("OfflineCoinsVal") * (24 * 60);
}
}
}
Awake()orStart()of a scene script, but from something that persists between scenes. - Draco18s no longer trusts SE