PlayerPrefs In Unity

Last Updated : 4 May, 2026

PlayerPrefs saves data as key-value pairs. You give each piece of data a unique key name and a value. Unity stores this data in a file on the player's device.

  • Data stays even after closing the game.
  • Works on Windows, Mac, Linux, Android, iOS, WebGL.
  • Best for small data (settings, high scores, level unlocks).

What Data Can PlayerPrefs Save?

PlayerPrefs supports only these three data types:

  • int: Example: 100, 9999 - Used for score, level number, coin count.
  • float: Example: 0.75, 1.5 - Used for volume and settings.
  • string: Example: "Player1", "Easy" - Used for player name and difficulty.

Saving Data (Set)

Use these methods to save data:

C#
// Save an integer
PlayerPrefs.SetInt("HighScore", 2500);
// Save a float
PlayerPrefs.SetFloat("MusicVolume", 0.8f);
// Save a string
PlayerPrefs.SetString("PlayerName", "Hero");
// Important! Saves to disk immediately (optional but recommended)
PlayerPrefs.Save();

SetInt(), SetFloat(), SetString() store the value in memory. Save() writes it to disk. Without Save(), data may be lost if game crashes.

Loading Data (Get)

Use these methods to load previously saved data:

C#
// Load integer (default value 0 if not found)
int highScore = PlayerPrefs.GetInt("HighScore", 0);
// Load float (default value 0.5f if not found)
float volume = PlayerPrefs.GetFloat("MusicVolume", 0.5f);
// Load string (default value "Guest" if not found)
string playerName = PlayerPrefs.GetString("PlayerName", "Guest");

The second parameter is the default value if the key doesn't exist. Always provide a default value.

Checking if a Key Exists

Sometimes you need to check if data was saved before

C#
if (PlayerPrefs.HasKey("HighScore"))
{
    // Data exists, load it
    int score = PlayerPrefs.GetInt("HighScore");
}
else
{
    // First time playing, use default
    int score = 0;
}

HasKey() returns true if the key exists in PlayerPrefs.

Deleting Data

Delete specific keys or all data:

C#
// Delete a specific key
PlayerPrefs.DeleteKey("HighScore");
// Delete all saved data (use carefully!)
PlayerPrefs.DeleteAll();

DeleteKey() removes one key. DeleteAll() clears everything - useful for a "Reset Game" button.

Complete Example: High Score System

C#
using UnityEngine;

public class HighScoreSystem : MonoBehaviour
{
    private int currentScore;
    private int highScore;

    void Start()
    {
        // Load high score when game starts
        highScore = PlayerPrefs.GetInt("HighScore", 0);
        Debug.Log("High Score: " + highScore);
    }

    public void AddScore(int points)
    {
        currentScore += points;

        // Check if new high score
        if (currentScore > highScore)
        {
            highScore = currentScore;
            PlayerPrefs.SetInt("HighScore", highScore);
            PlayerPrefs.Save();
            Debug.Log("New High Score!");
        }
    }

    public void ResetHighScore()
    {
        PlayerPrefs.DeleteKey("HighScore");
        highScore = 0;
        Debug.Log("High Score Reset");
    }
}

Where Does PlayerPrefs Save Data?

  • Windows: Stored in Registry: HKEY_CURRENT_USER\Software\Unity
  • Mac: Stored in Preferences folder: ~/Library/Preferences/
  • Android: Stored in app data: /data/data/com.companyname.appname/shared_prefs
  • iOS: Stored using UserDefaults

Limitations of PlayerPrefs

  • Not suitable for large data or full game states.
  • Easily editable by players, so not secure.
  • Cannot directly store complex data like lists or objects.
  • Stores data locally without cloud sync.
Comment
Article Tags:

Explore