I have the following code. The problem is, if this is a new game, when Load gets called it doesn't enter the if statement- if (!File.Exists(getPath())) - because somehow a file already exists even though the player has never played the game before. How can I fix this? Im using unity on a mac and the game is meant for ios.

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

public static class SaveSystem
{
    public static void Save(testGameData data)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream fs = new FileStream(getPath(), FileMode.Create);
        formatter.Serialize(fs, data);
        fs.Close();
    }

    public static testGameData Load()
    {
        Debug.Log(getPath());
        if (!File.Exists(getPath()))
        {
            testGameData emptyData = new testGameData();
            Save(emptyData);
            return emptyData;
        }
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream fs = new FileStream(getPath(), FileMode.Open);
        testGameData data = formatter.Deserialize(fs) as testGameData;
        fs.Close();
        return data;
    }


    private static string getPath()
    {
        return Application.persistentDataPath + "/data.qnd";
    }
}

When I delete the file manually it works, so I know thats the issue. obviously that isnt a solution if I want to publish this game.

0

There are 0 best solutions below