Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to save the player's score using PlayerPrefs.
Unity
PlayerPrefs.SetInt("PlayerScore", [1]);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined.
Forgetting to use quotes around the key name.
✗ Incorrect
We use playerScore as the variable holding the player's current score to save it.
2fill in blank
mediumComplete the code to load the player's score from PlayerPrefs.
Unity
int loadedScore = PlayerPrefs.[1]("PlayerScore", 0);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
GetString instead of GetInt.Using
SetInt which saves data instead of loading.✗ Incorrect
We use GetInt to retrieve an integer value saved with PlayerPrefs.
3fill in blank
hardFix the error in the code to save the player's level progress.
Unity
PlayerPrefs.SetInt("PlayerLevel", [1]); PlayerPrefs.[2]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same option for both blanks.
Not calling
Save() after setting values.✗ Incorrect
The first blank should be the variable playerLevel to save the level number. The second blank should be Save to write changes.
4fill in blank
hardFill both blanks to check if a saved score exists and then load it.
Unity
if (PlayerPrefs.[1]("PlayerScore")) { int score = PlayerPrefs.[2]("PlayerScore"); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
SetInt instead of GetInt.Not checking if the key exists before loading.
✗ Incorrect
Use HasKey to check if the key exists, then GetInt to load the score.
5fill in blank
hardFill all three blanks to save player's name, score, and then save changes.
Unity
PlayerPrefs.[1]("PlayerName", playerName); PlayerPrefs.[2]("PlayerScore", playerScore); PlayerPrefs.[3]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up
SetString and SetInt.Forgetting to call
Save().✗ Incorrect
Use SetString for the name, SetInt for the score, and Save to write changes.