'How to store a binary file in an ios app made with unity?

I'm making a game with levels, to store which level the user has reached, I used a binary file. The problem is, when I run it inside of unity, it works perfectly fine.

However, when I build it for IOS, there appears to be an error saying that my binary file has not been found:

Save file not found in /var/mobile/Containers/Data/Application/35DB4F8D-9528-4178-908F-7C3F5F7E6A69/Documents/binary.file

This is the code that writes this file:

using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public static class SaveSystem
{
    public static void SavePlayer (Player player)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        string path = Application.persistentDataPath + "/binary.file";

        FileStream stream = new FileStream(path, FileMode.Create);

        PlayerData data = new PlayerData(player);

        formatter.Serialize(stream, data);
        stream.Close();

    }

    public static PlayerData LoadPlayer ()
    {
        string path = Application.persistentDataPath + "/binary.file";
        if (File.Exists(path))
        {
            BinaryFormatter formatter = new BinaryFormatter();
            FileStream stream = new FileStream(path, FileMode.Open);

            PlayerData data = formatter.Deserialize(stream) as PlayerData;
            stream.Close();

            return data;


        }
        else
        {
            Debug.LogError("Save file not found in " + path);
            return null;
        } 
    }
}

Does anyone know how to fix this error because I would really like to publish my game when it is finished and I won't be able to do that until this error is fixed.

Thank you in advance

Emiel



Solution 1:[1]

Your save file and your load file have different names, one of them is binary.file and the other one is player.fun. Shouldn't these two be the same name? Also insted of using / as directory separator:

string path = Application.persistentDataPath + "/player.fun";

you better use this:

string path = Application.persistentDataPath + Path.DirectorySeparatorChar + "player.fun";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Armin