Concept Flow - PlayerPrefs for simple data
Start
Set Key-Value
Save Data
Retrieve Data
Use Data in Game
End
PlayerPrefs stores simple data as key-value pairs, saving and retrieving values for game use.
PlayerPrefs.SetInt("score", 10); PlayerPrefs.Save(); int score = PlayerPrefs.GetInt("score", 0); Debug.Log(score);
| Step | Action | Key | Value | Result/Output |
|---|---|---|---|---|
| 1 | SetInt called | score | 10 | Value stored in PlayerPrefs memory |
| 2 | Save called | - | - | Data saved to disk |
| 3 | GetInt called | score | - | Returns 10 |
| 4 | Debug.Log called | - | - | Prints 10 to Console |
| 5 | End | - | - | Execution complete |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| score | undefined | 10 (in PlayerPrefs memory) | 10 (saved) | 10 (retrieved) | 10 (used) |
PlayerPrefs stores simple data as key-value pairs. Use SetInt, SetFloat, SetString to save data. Call PlayerPrefs.Save() to write data to disk. Use GetInt, GetFloat, GetString to retrieve data with a default. Good for small, simple game settings or scores.
PlayerPrefs in Unity?SetInt with key and value.SetInt. Others use invalid method names.PlayerPrefs.SetString("playerName", "Alex");
string name = PlayerPrefs.GetString("playerName", "Unknown");
Debug.Log(name);PlayerPrefs.SetInt("level", 5);
int currentLevel = PlayerPrefs.GetInt("Level", 1);
Debug.Log(currentLevel);SetInt with key "highScore" and value 150, then call PlayerPrefs.Save() to ensure data is saved immediately.GetInt with key "highScore" and default 0 to load the saved score or 0 if not found.Pass the test and this pattern moves unknown