Bird
Raised Fist0
Unityframework~20 mins

PlayerPrefs for simple data in Unity - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PlayerPrefs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PlayerPrefs code?

Consider this Unity C# code snippet that uses PlayerPrefs:

PlayerPrefs.SetInt("score", 10);
PlayerPrefs.SetString("playerName", "Alex");
int score = PlayerPrefs.GetInt("score");
string name = PlayerPrefs.GetString("playerName");
Debug.Log($"Name: {name}, Score: {score}");

What will be printed in the Unity Console?

Unity
PlayerPrefs.SetInt("score", 10);
PlayerPrefs.SetString("playerName", "Alex");
int score = PlayerPrefs.GetInt("score");
string name = PlayerPrefs.GetString("playerName");
Debug.Log($"Name: {name}, Score: {score}");
AName: Alex, Score: 10
BName: , Score: 0
CName: Alex, Score: 0
DName: , Score: 10
Attempts:
2 left
💡 Hint

PlayerPrefs stores and retrieves data by key. The values set are returned when you get them.

❓ Predict Output
intermediate
2:00remaining
What happens if a PlayerPrefs key does not exist?

Look at this code snippet:

int level = PlayerPrefs.GetInt("level");
Debug.Log(level);

Assuming "level" was never set before, what will be printed?

Unity
int level = PlayerPrefs.GetInt("level");
Debug.Log(level);
A0
Bnull
C-1
DThrows an exception
Attempts:
2 left
💡 Hint

PlayerPrefs.GetInt returns a default value if the key is missing.

🔧 Debug
advanced
3:00remaining
Why does this PlayerPrefs code not save the data?

Check this code snippet:

PlayerPrefs.SetInt("lives", 3);
// Missing PlayerPrefs.Save()
// Later in the game, trying to get the value:
int lives = PlayerPrefs.GetInt("lives");
Debug.Log(lives);

Why might the value not be saved between game sessions?

Unity
PlayerPrefs.SetInt("lives", 3);
// Missing PlayerPrefs.Save()
// Later in the game, trying to get the value:
int lives = PlayerPrefs.GetInt("lives");
Debug.Log(lives);
ABecause PlayerPrefs keys must be declared before use.
BBecause PlayerPrefs.SetInt only works for strings, not integers.
CBecause PlayerPrefs.GetInt requires a default value parameter.
DBecause PlayerPrefs.Save() was not called, changes may not be written to disk immediately.
Attempts:
2 left
💡 Hint

Think about when PlayerPrefs writes data to permanent storage.

📝 Syntax
advanced
2:00remaining
Which option correctly deletes a PlayerPrefs key?

Which of the following code snippets correctly deletes the PlayerPrefs key "highscore"?

APlayerPrefs.ClearKey("highscore");
BPlayerPrefs.Remove("highscore");
CPlayerPrefs.DeleteKey("highscore");
DPlayerPrefs.Delete("highscore");
Attempts:
2 left
💡 Hint

Check the exact method name for deleting a key in PlayerPrefs.

🚀 Application
expert
3:00remaining
How many items are stored after this PlayerPrefs code runs?

Consider this sequence of PlayerPrefs commands:

PlayerPrefs.SetInt("coins", 100);
PlayerPrefs.SetString("player", "Sam");
PlayerPrefs.SetFloat("volume", 0.75f);
PlayerPrefs.DeleteKey("coins");
PlayerPrefs.SetInt("coins", 50);

How many keys are stored in PlayerPrefs after this code executes?

A1
B3
C2
D4
Attempts:
2 left
💡 Hint

Think about keys added, deleted, and re-added.

Practice

(1/5)
1. What is the main purpose of PlayerPrefs in Unity?
easy
A. To create 3D models in the game
B. To manage game physics calculations
C. To handle player input controls
D. To save small data like numbers and text between game sessions

Solution

  1. Step 1: Understand PlayerPrefs role

    PlayerPrefs is designed to store simple data like numbers and text that persist between game sessions.
  2. Step 2: Compare options with PlayerPrefs purpose

    Options A, C, and D relate to other game systems, not data storage.
  3. Final Answer:

    To save small data like numbers and text between game sessions -> Option D
  4. Quick Check:

    PlayerPrefs stores simple data = B [OK]
Hint: PlayerPrefs saves small data between sessions [OK]
Common Mistakes:
  • Confusing PlayerPrefs with input or physics systems
  • Thinking PlayerPrefs handles large files or complex data
2. Which of the following is the correct way to save an integer value 10 with key "score" using PlayerPrefs?
easy
A. PlayerPrefs.SetInt("score", 10);
B. PlayerPrefs.SaveInt("score", 10);
C. PlayerPrefs.Set("score", 10);
D. PlayerPrefs.PutInt("score", 10);

Solution

  1. Step 1: Recall PlayerPrefs method for integers

    The correct method to save an integer is SetInt with key and value.
  2. Step 2: Check method names in options

    Only PlayerPrefs.SetInt("score", 10); uses the correct method name SetInt. Others use invalid method names.
  3. Final Answer:

    PlayerPrefs.SetInt("score", 10); -> Option A
  4. Quick Check:

    SetInt saves integer = A [OK]
Hint: Use SetInt to save integers in PlayerPrefs [OK]
Common Mistakes:
  • Using incorrect method names like Set or PutInt
  • Forgetting to use quotes around the key string
3. What will be the output of this code snippet?
PlayerPrefs.SetString("playerName", "Alex");
string name = PlayerPrefs.GetString("playerName", "Unknown");
Debug.Log(name);
medium
A. Unknown
B. Error
C. Alex
D. null

Solution

  1. Step 1: Understand SetString and GetString usage

    The code saves "Alex" with key "playerName" and then retrieves it with a default value "Unknown".
  2. Step 2: Determine what GetString returns

    Since "playerName" exists, GetString returns "Alex", not the default "Unknown".
  3. Final Answer:

    Alex -> Option C
  4. Quick Check:

    GetString returns saved value = D [OK]
Hint: GetString returns saved value if key exists [OK]
Common Mistakes:
  • Assuming default value is always returned
  • Confusing SetString with SetInt or other methods
4. Identify the error in this code snippet:
PlayerPrefs.SetInt("level", 5);
int currentLevel = PlayerPrefs.GetInt("Level", 1);
Debug.Log(currentLevel);
medium
A. Key names are case-sensitive, causing default value to be returned
B. SetInt cannot be used for integers
C. GetInt requires a string key and an integer default value
D. PlayerPrefs.Save() is missing, so data won't save

Solution

  1. Step 1: Compare key names used in SetInt and GetInt

    SetInt uses key "level" (lowercase), GetInt uses "Level" (uppercase L). Keys are case-sensitive.
  2. Step 2: Understand effect of key mismatch

    Because keys differ, GetInt does not find "Level" and returns default 1 instead of 5.
  3. Final Answer:

    Key names are case-sensitive, causing default value to be returned -> Option A
  4. Quick Check:

    Case-sensitive keys cause mismatch = C [OK]
Hint: Check key spelling and case carefully [OK]
Common Mistakes:
  • Ignoring case sensitivity of keys
  • Thinking PlayerPrefs.Save() is always required immediately
5. You want to save a player's high score and retrieve it later. Which sequence of code correctly saves the score 150 and then loads it with a default of 0 if not found?
hard
A. PlayerPrefs.SetString("highScore", "150"); int score = PlayerPrefs.GetInt("highScore", 0);
B. PlayerPrefs.SetInt("highScore", 150); PlayerPrefs.Save(); int score = PlayerPrefs.GetInt("highScore", 0);
C. PlayerPrefs.SetInt(150, "highScore"); int score = PlayerPrefs.GetInt("highScore");
D. PlayerPrefs.SetInt("highScore", 150); int score = PlayerPrefs.GetInt("highScore", 100); PlayerPrefs.Save();

Solution

  1. Step 1: Save the high score correctly

    Use SetInt with key "highScore" and value 150, then call PlayerPrefs.Save() to ensure data is saved immediately.
  2. Step 2: Retrieve the high score with default

    Use GetInt with key "highScore" and default 0 to load the saved score or 0 if not found.
  3. Final Answer:

    PlayerPrefs.SetInt("highScore", 150); PlayerPrefs.Save(); int score = PlayerPrefs.GetInt("highScore", 0); -> Option B
  4. Quick Check:

    SetInt + Save + GetInt with default = A [OK]
Hint: Save with SetInt + Save(), load with GetInt and default [OK]
Common Mistakes:
  • Mixing SetString with GetInt causing type mismatch
  • Not calling PlayerPrefs.Save() after setting data
  • Swapping key and value parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard