0
0
Unityframework~20 mins

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

Choose your learning style9 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.