A file-based save system lets your game remember player progress by saving data to a file on the device. This way, players can continue where they left off even after closing the game.
File-based save system in Unity
using System.IO; using UnityEngine; public class SaveSystem { private string filePath = Application.persistentDataPath + "/savefile.json"; public void Save(string data) { File.WriteAllText(filePath, data); } public string Load() { if (File.Exists(filePath)) { return File.ReadAllText(filePath); } return null; } }
Use Application.persistentDataPath to get a safe place to save files on any device.
Use File.WriteAllText to save text data and File.ReadAllText to load it back.
File.WriteAllText(filePath, "Hello World");
string content = File.ReadAllText(filePath);string path = Application.persistentDataPath + "/playerdata.json"; if (File.Exists(path)) { string json = File.ReadAllText(path); }
This program saves a player's level and health to a JSON file and then loads it back, printing the values to the Unity console.
using System.IO; using UnityEngine; [System.Serializable] public class PlayerData { public int level; public float health; } public class SaveSystemExample : MonoBehaviour { private string filePath; void Start() { filePath = Application.persistentDataPath + "/playerdata.json"; // Create some data PlayerData data = new PlayerData { level = 5, health = 75.5f }; // Convert to JSON string json = JsonUtility.ToJson(data); // Save to file File.WriteAllText(filePath, json); // Load from file if (File.Exists(filePath)) { string loadedJson = File.ReadAllText(filePath); PlayerData loadedData = JsonUtility.FromJson<PlayerData>(loadedJson); Debug.Log($"Loaded Level: {loadedData.level}, Health: {loadedData.health}"); } } }
Always check if the file exists before loading to avoid errors.
Use JSON format to save complex data like player stats in a readable way.
Files saved in Application.persistentDataPath stay between game sessions and app updates.
A file-based save system stores game data on the device so progress is not lost.
Use File.WriteAllText and File.ReadAllText with Application.persistentDataPath for safe saving and loading.
JSON is a simple format to save and load structured data like player stats.
Practice
Solution
Step 1: Understand the role of saving data
Saving data means keeping player progress or settings so they are not lost when the game closes.Step 2: Identify file-based save system purpose
A file-based save system stores this data on the device's storage, allowing the game to load it later.Final Answer:
To store game data on the device so progress is not lost -> Option AQuick Check:
Save system = store progress [OK]
- Confusing saving with graphics or performance
- Thinking saving connects to online servers
- Assuming saving only affects frame rate
Solution
Step 1: Identify the correct method to write text
The method to write text to a file is File.WriteAllText.Step 2: Use the correct file path
Application.persistentDataPath is the safe path for saving files on the device.Final Answer:
File.WriteAllText(Application.persistentDataPath + "/savefile.txt", data); -> Option AQuick Check:
WriteAllText + persistentDataPath = correct save [OK]
- Using File.ReadAllText to write data
- Using Application.dataPath which is not safe for saving
- Using non-existent methods like File.SaveText
string path = Application.persistentDataPath + "/savefile.json"; string json = File.ReadAllText(path); PlayerData data = JsonUtility.FromJson<PlayerData>(json); Debug.Log(data.score);Assuming PlayerData is a class with a public int score field.
Solution
Step 1: Read JSON string from file
The file contains '{"score": 100}', so json will hold this string.Step 2: Deserialize JSON to PlayerData object
JsonUtility.FromJson<PlayerData>(json) creates an object with score = 100.Step 3: Output the score field
Debug.Log(data.score) prints 100 to the console.Final Answer:
100 -> Option DQuick Check:
Deserialize JSON score = 100 [OK]
- Assuming score is zero if not initialized
- Confusing ReadAllText with WriteAllText
- Expecting null instead of a valid object
PlayerData data = new PlayerData();
data.score = 50;
string json = JsonUtility.ToJson(data);
File.WriteAllText("savefile.json", json);Solution
Step 1: Check file path usage
The code writes to "savefile.json" without specifying a full path, which may cause saving in an unsafe or unexpected location.Step 2: Use Application.persistentDataPath for safe saving
Unity recommends using Application.persistentDataPath + "/savefile.json" to save files safely on all platforms.Final Answer:
Missing Application.persistentDataPath in file path -> Option BQuick Check:
Always use persistentDataPath for file saving [OK]
- Saving files without full path causes errors
- Thinking JsonUtility cannot serialize simple classes
- Confusing WriteAllText input types
Solution
Step 1: Use a class to hold multiple stats
A class like PlayerStats with fields score, level, health organizes data for easy serialization.Step 2: Serialize and save correctly
JsonUtility.ToJson converts the class to JSON string; File.WriteAllText with Application.persistentDataPath saves it safely.Final Answer:
Create a PlayerStats class with fields, serialize it with JsonUtility.ToJson, then save with File.WriteAllText using Application.persistentDataPath -> Option CQuick Check:
Class + JsonUtility + WriteAllText + persistentDataPath = correct [OK]
- Saving stats separately without JSON causes complexity
- Mixing up ReadAllText and WriteAllText roles
- Using FromJson for saving instead of loading
Ready to prove it?
Pass the test and this pattern moves unknown
