PlayerPrefs – Saving data in Unity

PlayerPrefs is a basic, still quite powerful, way of saving some data in our app. We can save values of type: int, float and string. We need to give our PlayerPrefs a name so that we can call it later on. First we should check if there is a PlayerPrefs of that name already created and assign a starting value on creation.

        int level;
        if (PlayerPrefs.HasKey("levelPlayerPrefs"))
            level = PlayerPrefs.GetInt("levelPlayerPrefs");
            level = 1;
            PlayerPrefs.SetInt("levelPlayerPrefs", level);

And this is all we really need. If the PlayerPref of that name exists, get it’s value. If it doesn’t exist, create one and assign some value. If we want to save the level count later on in the game we just use PlayerPrefs.SetInt() method giving a name and new value as arguments.

Leave a Comment

Scroll to Top