How to use PlayerPrefs

Hey guys, i started to work on a simple game yesterday, and it’s almost done, but i wanted to implement a Shop for the Player to buy things. I already made a system to store how many points the Player has, but i don’t know how to save a float after the game is closed, and then restore it once it opens. I’ve been reading a little, and i discovered that there is one way, and you gotta use PlayerPrefs, but i never used it, and i didn’t get what am i supposed to do and how. Could someone explain to me how would i use PlayerPrefs to save a variable once the game is closed, and then restore it once it is opened again?

Thank you kindly =]


PlayerPrefs.SetString("username","John Doe");

// If you need boolean value:

bool val = true;
PlayerPrefs.SetInt("PropName", val ? 1 : 0);


int score = PlayerPrefs.GetInt("score");
float volume = PlayerPrefs.GetFloat("volume");
string player = PlayerPrefs.GetString("username");

bool val = PlayerPrefs.GetInt("PropName") == 1 ? true : false;

Playerprefs is meant for saving player preferences; you wouldn’t want to use it to save high scores and progress data, because it’s not difficult for players to edit the playerprefs data.

There’s a couple of ways to save data safely, and this official unity tutorial video goes over one of them, using C#'s BinaryFormatter.

At start of your game you use PlayerPrefs.GetInt(“Player level”); to restore your player level (or something else).

Than you play your game and…

On disable() you use PlayerPrefs.SetInt(“Player level”, currentLevel); to write you current value (player level etc.).

It is an example of using PlayerPrefs with simple player level up system:

public class ExperienceLevelSystem : MonoBehaviour {
        public static int currentXP;
        private void Start()
            if (PlayerPrefs.HasKey("currentXP"))
                currentXP = PlayerPrefs.GetInt("currentXP");
        private void Update()
            if (currentXP >= maxXP)
        public void LevelUpSystem()
            //currentXP = currentXP - maxXP;
            maxXP = 200 + 200 * (currentLevel * currentLevel);
            Time.timeScale = 0;
        public static void Add(int pointsToAdd)
            currentXP += pointsToAdd;
        void OnDisable()
            PlayerPrefs.SetInt("currentLevel", currentLevel);

i think this video will be helpfull