Saving time played for every game/session, and calculate average time and display it in another scene

Hi, I am trying to save time for every session/game and then calculate average time and update it in another scene. I am gonna use PlayerPrefs, but not sure how to aproach it… saving the time every time the game ends (p1 win, p2 win or draw) and save it to Player prefs, but that way I will just update the value, not save multiple values…can anyone help?

Take Time.time at the start and the end of a session/scene. The difference is the time played.

For an average you don’t need all values. More commonly you’d use a “running average” which means the average of the past n number of plays.

PlayerPrefs is generally a crutch to save anything even decently complex. You can easily save to Json with JsonUtility and a simple class holding an array or list with the (most recent) playtime values. The average time itself needn’t be saved, you can calculate that when you need it by taking the average. Unless you go over several thousands of play sessions (hence: running average) this is nothing to worry about performance wise.

1 Like

Thank you CodeSmile,

Do you have an example please?

My game is simple X/O, I save the Wins, session count in PlayerPrefs and just need AVG time per session. I used Debug.Log to display time per game when player 1 or two wins or is a draw, just need to save the times so I can find the average and display it in the main scene.

All the help is much appreciated, thank you again!