Saving and loading player data – using PlayerPrefs
While the previous recipe illustrates how the static properties allow a game to remember values between different scenes, these values are forgotten once the game application has quit. Unity provides the PlayerPrefs
feature to allow a game to store and retrieve data, between the different game playing sessions.
Getting ready
This recipe builds upon the previous recipe. In case you haven't completed the previous recipe, we have included a Unity
package named game_scoreStaticVariables
in the the 1362_10_05
folder. In order to follow this recipe using this package, you must do the following:
- Create a new 2D project and import the
game_HigherOrLower
package. - Add each of the scenes to the build in the sequence (
scene0_mainMenu
, thenscene1_gamePlaying
, and so on).
How to do it...
To save and load the player data using PlayerPrefs
, follow these steps:
- Delete the C# script called
Player
. - Edit the C# script called
UpdateScoreText
by replacing...