0
0
Unityframework~20 mins

Save slot management in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Save Slot 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 save slot check?

Consider this Unity C# code snippet that checks if a save slot is empty or not. What will be printed to the console?

Unity
string slotKey = "SaveSlot1";
if (PlayerPrefs.HasKey(slotKey))
{
    Debug.Log("Slot occupied");
}
else
{
    Debug.Log("Slot empty");
}
ASlot occupied
BSlot empty
CNo output
DRuntime error
Attempts:
2 left
💡 Hint

Think about what PlayerPrefs.HasKey returns if the key does not exist.

Predict Output
intermediate
2:00remaining
What does this code print after saving data?

This code saves a player's score in slot 2 and then reads it back. What will be printed?

Unity
int slot = 2;
string key = $"SaveSlot{slot}_Score";
PlayerPrefs.SetInt(key, 150);
int score = PlayerPrefs.GetInt(key, 0);
Debug.Log(score);
A150
Bnull
C0
DRuntime error
Attempts:
2 left
💡 Hint

Remember that SetInt stores the value and GetInt retrieves it.

🔧 Debug
advanced
2:00remaining
Why does this save slot loading code fail?

Look at this code that tries to load a player's name from a save slot. It throws a NullReferenceException. Why?

Unity
string slotKey = "SaveSlot3_Name";
string playerName = PlayerPrefs.GetString(slotKey, null);
int nameLength = playerName.Length;
Debug.Log(nameLength);
APlayerPrefs.GetString returns null if key missing, causing NullReferenceException on Length
BPlayerPrefs.GetString throws exception if key missing
CThe variable slotKey is not initialized
DplayerName is empty string, so Length is zero, no exception
Attempts:
2 left
💡 Hint

What happens if you call GetString with a key that does not exist?

📝 Syntax
advanced
2:00remaining
Which option correctly saves and loads a boolean in a save slot?

Unity's PlayerPrefs does not have a direct method for booleans. Which code correctly saves and loads a boolean value?

A
PlayerPrefs.SetFloat("SaveSlot1_Completed", completed ? 1.0f : 0.0f);
bool completed = PlayerPrefs.GetFloat("SaveSlot1_Completed") == 1.0f;
B
PlayerPrefs.SetBool("SaveSlot1_Completed", completed);
bool completed = PlayerPrefs.GetBool("SaveSlot1_Completed");
C
PlayerPrefs.SetString("SaveSlot1_Completed", completed.ToString());
bool completed = bool.Parse(PlayerPrefs.GetString("SaveSlot1_Completed"));
D
PlayerPrefs.SetInt("SaveSlot1_Completed", completed ? 1 : 0);
bool completed = PlayerPrefs.GetInt("SaveSlot1_Completed") == 1;
Attempts:
2 left
💡 Hint

PlayerPrefs supports int, float, and string but not bool directly.

🚀 Application
expert
3:00remaining
How many save slots are created by this code?

This code saves player data for multiple slots. How many distinct save slots are created?

Unity
for (int i = 0; i < 3; i++)
{
    PlayerPrefs.SetInt($"Slot{i}_Level", i + 1);
    PlayerPrefs.SetString($"Slot{i}_Name", $"Player{i}");
}

int count = 0;
for (int i = 0; i < 5; i++)
{
    if (PlayerPrefs.HasKey($"Slot{i}_Level"))
        count++;
}
Debug.Log(count);
A0
B5
C3
D2
Attempts:
2 left
💡 Hint

Check how many keys are set and how many are checked.