DEV Community

Cover image for PlayerPrefs in Unity and how to use it
Ben
Ben

Posted on

PlayerPrefs in Unity and how to use it

PlayerPrefs is a built-in class in Unity that provides an easy and native way to store and retrieve values. It can be used to save data such as user preferences, game progress, high scores, or any other data that you want to persist even after the game has been closed.

To be more specific, PlayerPrefs uses a simple key-value structure, where you can store/retrieve values by specifying a key name.

Here is an example on how to use PlayerPrefs in Unity:

// Store the high score
int highScore = 1000;
PlayerPrefs.SetInt("HighScore", highScore);

// Retrieve the high score
int storedHighScore = PlayerPrefs.GetInt("HighScore", 0);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)