Life countdown for unity using PlayerPrefs?
My playerPrefs arent saving the time for the life countdown correctly. Is there a sample code on this topic? where i can refer to?
Also, if i save my time in playerprefs, then willl it still counting when exited the game?
Answer by Priyanka-Rajwanshi · Apr 01, 2020 at 11:06 AM
In order to calculate the time that was lapsed since you last shut down the game, you should save the last time playerprefs in the function OnApplicationPause and calcuate the timelapsed in the Awake Function
void OnApplicationPause(bool isPause){
if (isPause)
{
//save the system time at which the application went in background
PlayerPrefs.SetString("Timer", DateTime.Now.ToString());
}
}
void Awake(){
float timerLapsed = (float)(System.DateTime.Now - Convert.ToDateTime(PlayerPrefs.GetString("Timer"))).TotalSeconds;
}
For those who are still looking out to understand how to create a life counter, you can check out the following link: http://codesaying.com/life-counter-in-unity/
Your answer
Follow this Question
Related Questions
How i store game unlocked levels unlock also after memory clean (Android unity game) 0 Answers
Set the username with an inputfield ? 3 Answers
My save data is being overwritten 0 Answers
High-score Saving Issue 2 Answers
PlayerPrefs mad?! 0 Answers