In game development, why is it important to use persistence to save player progress?
Think about what happens when you close a game and open it later.
Persistence saves the player's progress so they can continue where they left off, keeping achievements, levels, and settings intact.
Consider this Unity C# code snippet that saves and loads a player's score:
PlayerPrefs.SetInt("score", 150);
int savedScore = PlayerPrefs.GetInt("score", 0);
Debug.Log(savedScore);What will be printed in the console?
PlayerPrefs.SetInt("score", 150); int savedScore = PlayerPrefs.GetInt("score", 0); Debug.Log(savedScore);
PlayerPrefs.GetInt returns the saved value or the default if not found.
The code saves 150 under the key "score" and then retrieves it, so 150 is printed.
Look at this Unity C# code snippet meant to save player health:
int playerHealth = 80;
PlayerPrefs.SetInt("health", playerHealth);
// Missing line here
int loadedHealth = PlayerPrefs.GetInt("health", 100);
Debug.Log(loadedHealth);Why might the saved health not persist after restarting the game?
Think about when PlayerPrefs actually writes data permanently.
PlayerPrefs data is cached and only saved to disk when PlayerPrefs.Save() is called or when the application quits. Without calling Save(), data might not persist.
Choose the correct syntax to save a player's name "Alex" using PlayerPrefs in Unity C#.
Remember PlayerPrefs has different methods for different data types.
PlayerPrefs.SetString takes a key and a string value. Option D uses correct syntax and types.
Consider this code snippet:
PlayerPrefs.SetInt("level", 5);
PlayerPrefs.SetFloat("volume", 0.8f);
PlayerPrefs.SetString("playerName", "Sam");
PlayerPrefs.SetInt("level", 6);
How many unique keys are stored in PlayerPrefs after this runs?
Think about what happens when you set a value for an existing key.
There are three keys: "level", "volume", and "playerName". The "level" key is overwritten, not duplicated.