Unity Foundations·From empty scene to playable build

Saving and loading

Store player progress as JSON in a folder that survives, and version the format.

Beginner 13 min read Lesson 5 of 300 Challenge included
What you will be able to do
  • Keep PlayerPrefs for small preferences, not whole save games.
  • Serialize a save class to JSON with JsonUtility.
  • Write files to Application.persistentDataPath.
  • Include a version number so old saves can migrate.

In plain English

PlayerPrefs is a simple key-value store for small preferences, but it is not built for large or structured save data. JSON is a text format for describing objects, and JsonUtility turns a plain C# class into a JSON string and back. Application.persistentDataPath is a per-app folder that persists across sessions and is the right place for save files. Versioning a save means storing a version number in the file so you can migrate old saves when the format changes.

Step by step

  1. 1Define a serializable data class for the save.
  2. 2Serialize it to JSON with JsonUtility.
  3. 3Write the JSON to Application.persistentDataPath.
  4. 4Load the file back and deserialize it with FromJson.
  5. 5Add a version field so future changes can migrate.
SaveSystem.csC#
using System.IO;
using UnityEngine;

[System.Serializable]
public class SaveData
{
    public int score;
    public float version = 1f;
}

public class SaveSystem : MonoBehaviour
{
    private string SavePath => Path.Combine(Application.persistentDataPath, "save.json");

    public void Save(SaveData data)
    {
        string json = JsonUtility.ToJson(data);
        File.WriteAllText(SavePath, json);
    }

    public SaveData Load()
    {
        if (!File.Exists(SavePath)) return new SaveData();
        string json = File.ReadAllText(SavePath);
        return JsonUtility.FromJson<SaveData>(json);
    }
}

Common mistakes

  • Storing a whole save game in PlayerPrefs bloats the registry and slows loading; keep it for small preferences.
  • Saving to a hardcoded relative path works in the editor but fails on a device, where the game cannot write arbitrary folders.
  • Changing the shape of a save without a version number makes old saves crash or load garbage.
  • JsonUtility only serializes public or SerializeField fields, so private fields silently disappear from the save.
Your turn

Save a score

Medium

Write the Save method so it serializes SaveData to JSON and writes it to persistentDataPath.

CSHARP · starter
using System.IO;
using UnityEngine;

[System.Serializable]
public class SaveData
{
    public int score;
}

public class SaveSystem : MonoBehaviour
{
    private string SavePath => Path.Combine(Application.persistentDataPath, "save.json");

    public void Save(SaveData data)
    {
    }
}
Check your understanding before moving on.
Knowledge checkQuestion 1

Where should a save file be stored?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 5 of 300 · 0% of course