How do I add the previous time the player spent playing for example level 1 and the next time he played the same level will be added to the previous time, which will be shown on the result board.
For getting the time in seconds we used
timereasy1 += Time.deltaTime;
and then saving it to
PlayerPrefs.SetFloat("timer1",timereasy1);
and adding it to
totaltime=totaltime+timer1;
but it won't add up... It will still show the recent time spent of the player.
You need to store the value like you are but you can simplify it like so.
private float _timePlayed;
private void Awake()
{
//When the Game/Level starts get the previous amount of time played if there is a PlayerPref for it otherwise it defaults to 0 as an unset float
if (PlayerPrefs.HasKey("timer1"))
{
_timePlayed = PlayerPrefs.GetFloat("timer1");
}
}
private void Update()
{
//Update the Time played during Game Play
_timePlayed += Time.deltaTime;
}
private void GameOver()
{
//At the end of the Game/Level update the PlayerPref with the new value
PlayerPrefs.SetFloat("timer1", _timePlayed);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.