Weird save file... Am I doing it wrong?

Hello everyone,

I’m currently doing, for the first time, a save/load system and right now I’m only doing it for the game’s settings. I’m currently getting all the data from 3 different scriptable objects into a custom class (not monobehaviour) called “Settings”. Then I made that class a [System.Serializable] and used it on another class called “SaveManager”, that is part of my GameManager (GameObject that is not destroyed on load and has a public static instance).
My save and load methods look like this:

    private void SaveObject(object objectToSave, string path)
    {
        Debug.Log($"Saving to {path}...");

        BinaryFormatter binaryFormatter = new BinaryFormatter();
        FileStream file = new FileStream(Application.persistentDataPath + path, FileMode.Create);
      
        binaryFormatter.Serialize(file, objectToSave);
        file.Close();
    }

    private void LoadObject(object objectToLoad, string path)
    {
        Debug.Log($"Checking from {path} file...");
        if (File.Exists(Application.persistentDataPath + path))
        {
            Debug.Log($"Loading from {path}...");
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream file = new FileStream(Application.persistentDataPath + path, FileMode.Open);

            objectToLoad = binaryFormatter.Deserialize(file) as Settings;
            file.Close();
        }
    }

And are then called like this:

    public void SaveSettings()
    {
        Settings settings = new Settings(graphics, audio, keybindings);

        CheckSaveLocation(Action.save, "/Saved");
        CheckSaveLocation(Action.save, "/Saved/Settings");

        SaveObject(settings, "/Saved/Settings/settings");
    }

    public void LoadSettings()
    {
        Settings settings = new Settings(graphics, audio, keybindings);

        CheckSaveLocation(Action.load, "/Saved");
        CheckSaveLocation(Action.load, "/Saved/Settings");

        LoadObject(settings, "/Saved/Settings/settings");
    }

Now onto the problem:
It saves and loads without a problem but when I look into the saved file it does not look like it is saved as a binary file…

What I expect it to look like:

5007569--489713--upload_2019-9-27_18-14-57.png

What it looks like:

I feel like I’m doing something really wrong… Please enlighten me!

Thank you!

EDIT:

I tried to build the game and it does not save on build. So it works on editor but not on build… Weird! Any ideas?

The actual data is binary, but there are multiple ways to visualize binary data. The screen shot of what you “expect” to see is displayed in hexadecimal, where as the other screen shot looks like it’s displaying the data in unicode with annotations for white space. It could certain be the exact same data in each case, for all I know.

1 Like

Please show the Debug.Log statements when run on a device. They will show in the adb logcat logs.

I’ve tried connecting it to unity’s console and I get no errors, but either way, here goes the log file (changed its extension so it lets me upload it).

I actually have no idea why it’s working on the player but not on the build. It’s actually really, really weird but I probably made a mistake somewhere.

Also you referenced “adb logcat logs” but I’m developing for PC, not android, so I got the log from my AppData, I hope it is the one you asked for.

5010254–490046–Player.txt (23.2 KB)

After a lots of painful hours I figured that apparently I need to use a Json utility in order to save… I had no idea you had to use it in order to save and thought you could just serialize it to the specified fixed path and it would work… Apparently not…
Basically this was what changed:

    private void SaveObject(object objectToSave, string path)
    {
        Debug.Log($"Saving to {path}...");

        BinaryFormatter binaryFormatter = new BinaryFormatter();
        FileStream file = new FileStream(Application.persistentDataPath + path, FileMode.Create);

        string json = JsonUtility.ToJson(objectToSave);
        binaryFormatter.Serialize(file, json);
        file.Close();
    }

    private void LoadObject(object objectToLoad, string path)
    {
        Debug.Log($"Checking from {path} file...");
        if (File.Exists(Application.persistentDataPath + path))
        {
            Debug.Log($"Loading from {path}...");
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            FileStream file = new FileStream(Application.persistentDataPath + path, FileMode.Open);

            JsonUtility.FromJsonOverwrite((string)binaryFormatter.Deserialize(file), objectToLoad);
            file.Close();
        }
    }

To Save:
string json = JsonUtility.ToJson(objectToSave);

To Load:
JsonUtility.FromJsonOverwrite((string)binaryFormatter.Deserialize(file), objectToLoad);

Also noted that sometimes unity throws an error here:
JsonUtility.FromJsonOverwrite((string)binaryFormatter.Deserialize(file), objectToLoad);
Saying that there was a cast error and that the binaryFormatter cannot be a string… Why? I have no clue…
It goes away after you press play 2/3 times…