0
0
Unityframework~20 mins

File-based save system in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Save 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 Unity C# code saving a player's score?

Consider this Unity C# code that saves a player's score to a file and then reads it back. What will be printed in the console?

Unity
using System.IO;
using UnityEngine;

public class SaveLoadExample : MonoBehaviour
{
    void Start()
    {
        string path = Application.persistentDataPath + "/score.txt";
        File.WriteAllText(path, "150");
        string scoreText = File.ReadAllText(path);
        Debug.Log($"Score loaded: {scoreText}");
    }
}
AScore loaded: 150
BScore loaded: 0
CFileNotFoundException
DScore loaded: null
Attempts:
2 left
💡 Hint

Think about what File.WriteAllText and File.ReadAllText do.

🧠 Conceptual
intermediate
1:30remaining
Which file path is best for saving game data in Unity?

In Unity, where should you save persistent game data to ensure it works on all platforms?

AApplication.persistentDataPath
BApplication.streamingAssetsPath
CApplication.dataPath
DApplication.temporaryCachePath
Attempts:
2 left
💡 Hint

Look for the path meant for permanent data storage.

🔧 Debug
advanced
2:30remaining
Why does this Unity save code throw an exception?

Look at this code snippet that tries to save player data. Why does it throw an exception?

Unity
using System.IO;
using UnityEngine;

public class SaveDebug : MonoBehaviour
{
    void SaveScore(int score)
    {
        string path = Application.persistentDataPath + "/save.txt";
        FileStream fs = new FileStream(path, FileMode.Open);
        StreamWriter writer = new StreamWriter(fs);
        writer.Write(score);
        writer.Close();
        fs.Close();
    }
}
AFileStream needs FileAccess.ReadWrite specified
BStreamWriter.Write cannot write integers
CApplication.persistentDataPath is read-only
DFileMode.Open throws exception if file does not exist
Attempts:
2 left
💡 Hint

Check what happens if the file is missing when opening with FileMode.Open.

📝 Syntax
advanced
2:00remaining
Which option correctly serializes and saves a PlayerData object to JSON in Unity?

Given a PlayerData class, which code snippet correctly saves it as JSON to a file?

Unity
public class PlayerData
{
    public int level;
    public float health;
}
A
string json = JsonUtility.FromJson(playerData);
File.WriteAllText(path, json);
B
string json = JsonUtility.ToJson(playerData);
File.ReadAllText(path, json);
C
string json = JsonUtility.ToJson(playerData);
File.WriteAllText(path, json);
D
string json = JsonUtility.ToString(playerData);
File.WriteAllText(path, json);
Attempts:
2 left
💡 Hint

Remember the method to convert an object to JSON is ToJson.

🚀 Application
expert
3:00remaining
How many files are created after running this Unity save system code?

This code saves multiple player profiles to separate files. How many files will be created after running it?

Unity
using System.IO;
using UnityEngine;

public class MultiSave : MonoBehaviour
{
    void SaveProfiles()
    {
        for (int i = 0; i < 3; i++)
        {
            string path = Application.persistentDataPath + $"/player_{i}.json";
            string json = JsonUtility.ToJson(new PlayerData { level = i, health = 100f });
            File.WriteAllText(path, json);
        }
    }
}

public class PlayerData
{
    public int level;
    public float health;
}
A1
B3
C0
D4
Attempts:
2 left
💡 Hint

Count how many times File.WriteAllText is called with different file names.