VOOZH about

URL: https://www.geeksforgeeks.org/c-sharp/playerprefs-in-unity/

⇱ PlayerPrefs In Unity - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:

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:

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

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

Deleting Data

Delete specific keys or all data:

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

Complete Example: High Score System

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:
Article Tags:

Explore