PlayerPrefs lets you save small pieces of data like scores or settings so they stay even after you close the game.
PlayerPrefs for simple data in Unity
PlayerPrefs.SetInt("key", value); PlayerPrefs.SetFloat("key", value); PlayerPrefs.SetString("key", "value"); int score = PlayerPrefs.GetInt("key", defaultValue); float volume = PlayerPrefs.GetFloat("key", defaultValue); string name = PlayerPrefs.GetString("key", "default"); PlayerPrefs.Save();
Use SetInt, SetFloat, or SetString to save data.
Use GetInt, GetFloat, or GetString to read data. You can give a default value if the key doesn't exist.
PlayerPrefs.SetInt("HighScore", 100); int score = PlayerPrefs.GetInt("HighScore", 0);
PlayerPrefs.SetString("PlayerName", "Alex"); string name = PlayerPrefs.GetString("PlayerName", "Guest");
PlayerPrefs.SetFloat("Volume", 0.75f); float vol = PlayerPrefs.GetFloat("Volume", 1.0f);
This Unity script saves a high score, player name, and music volume when the game starts. Then it reads them back and prints them to the console.
using UnityEngine; public class PlayerPrefsExample : MonoBehaviour { void Start() { // Save data PlayerPrefs.SetInt("HighScore", 250); PlayerPrefs.SetString("PlayerName", "Sam"); PlayerPrefs.SetFloat("MusicVolume", 0.5f); PlayerPrefs.Save(); // Load data int highScore = PlayerPrefs.GetInt("HighScore", 0); string playerName = PlayerPrefs.GetString("PlayerName", "Unknown"); float musicVolume = PlayerPrefs.GetFloat("MusicVolume", 1.0f); Debug.Log($"High Score: {highScore}"); Debug.Log($"Player Name: {playerName}"); Debug.Log($"Music Volume: {musicVolume}"); } }
PlayerPrefs is best for small, simple data like settings or scores, not big files or complex data.
Always call PlayerPrefs.Save() to make sure data is written to disk immediately.
Data saved with PlayerPrefs stays even if you close and reopen the game.
PlayerPrefs stores small data like numbers and text between game sessions.
Use Set methods to save and Get methods to load data.
Remember to call PlayerPrefs.Save() to save changes right away.
Practice
PlayerPrefs in Unity?Solution
Step 1: Understand PlayerPrefs role
PlayerPrefs is designed to store simple data like numbers and text that persist between game sessions.Step 2: Compare options with PlayerPrefs purpose
Options A, C, and D relate to other game systems, not data storage.Final Answer:
To save small data like numbers and text between game sessions -> Option DQuick Check:
PlayerPrefs stores simple data = B [OK]
- Confusing PlayerPrefs with input or physics systems
- Thinking PlayerPrefs handles large files or complex data
Solution
Step 1: Recall PlayerPrefs method for integers
The correct method to save an integer isSetIntwith key and value.Step 2: Check method names in options
Only PlayerPrefs.SetInt("score", 10); uses the correct method nameSetInt. Others use invalid method names.Final Answer:
PlayerPrefs.SetInt("score", 10); -> Option AQuick Check:
SetInt saves integer = A [OK]
- Using incorrect method names like Set or PutInt
- Forgetting to use quotes around the key string
PlayerPrefs.SetString("playerName", "Alex");
string name = PlayerPrefs.GetString("playerName", "Unknown");
Debug.Log(name);Solution
Step 1: Understand SetString and GetString usage
The code saves "Alex" with key "playerName" and then retrieves it with a default value "Unknown".Step 2: Determine what GetString returns
Since "playerName" exists, GetString returns "Alex", not the default "Unknown".Final Answer:
Alex -> Option CQuick Check:
GetString returns saved value = D [OK]
- Assuming default value is always returned
- Confusing SetString with SetInt or other methods
PlayerPrefs.SetInt("level", 5);
int currentLevel = PlayerPrefs.GetInt("Level", 1);
Debug.Log(currentLevel);Solution
Step 1: Compare key names used in SetInt and GetInt
SetInt uses key "level" (lowercase), GetInt uses "Level" (uppercase L). Keys are case-sensitive.Step 2: Understand effect of key mismatch
Because keys differ, GetInt does not find "Level" and returns default 1 instead of 5.Final Answer:
Key names are case-sensitive, causing default value to be returned -> Option AQuick Check:
Case-sensitive keys cause mismatch = C [OK]
- Ignoring case sensitivity of keys
- Thinking PlayerPrefs.Save() is always required immediately
Solution
Step 1: Save the high score correctly
UseSetIntwith key "highScore" and value 150, then callPlayerPrefs.Save()to ensure data is saved immediately.Step 2: Retrieve the high score with default
UseGetIntwith key "highScore" and default 0 to load the saved score or 0 if not found.Final Answer:
PlayerPrefs.SetInt("highScore", 150); PlayerPrefs.Save(); int score = PlayerPrefs.GetInt("highScore", 0); -> Option BQuick Check:
SetInt + Save + GetInt with default = A [OK]
- Mixing SetString with GetInt causing type mismatch
- Not calling PlayerPrefs.Save() after setting data
- Swapping key and value parameters
Ready to prove it?
Pass the test and this pattern moves unknown
