0

I'm using Unity 2017 and wish to serialize save game data like so:

public void Save(Statistics data)
{
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(Application.persistentDataPath
         + "/playerInfo.dat");
    bf.Serialize(file, data);
    file.Close();
}

The LevelData class is marked Serializable and contains only strings and ints. The class that implements IPersistentData meets those requirements too.

I did look for the problem and found that I should set an environment variable in my Awake method that I did, but that didn't solve anything. I'm using Windows 10 as operating system to develop on but the game will be made for Android.

When attempting to save, I always get this exception

SerializationException: Unexpected binary element: 255 System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info)

Any sort of advice is welcome to get around the issue.

EDIT

Updating after trying the hints of Programmer: The class I wish to serialize:

[Serializable]
public class Statistics
{
    public int LastPlayedLevel;
    public Statistics()
    {
        MusicVolume = 1f;
        SFXVolume = 1f;
        LevelDatas = new List<LevelData>();
        LastPlayedLevel = 1;
    }
    public float MusicVolume;
    public float SFXVolume;
    public List<LevelData> LevelDatas;
}

And the LevelData:

[Serializable]
public class LevelData
{
    public LevelData(int levelNumber,string name)
    {
        this.LevelNumber = levelNumber;
        this.Name = name;
        this.BestPercent = 0;
        this.DeathCount = 0;
    }
    public int LevelNumber;
    public string Name;
    public int BestPercent;
    public uint DeathCount;
}
agiro
  • 2,018
  • 2
  • 30
  • 62
  • 2
    You **cannot** serialize interface or auto property in Unity. See [this](http://answers.unity3d.com/questions/783456/solution-how-to-serialize-interfaces-generics-auto.html) post for workaround. – Programmer Aug 01 '17 at 15:23
  • If I just use the class instead of the interface, would it work? – agiro Aug 01 '17 at 15:28
  • 1
    It should. Note that you have auto property. Last time I checked Unity couldn't serialize that so remove that. I mean the `{ get; set; }`.... – Programmer Aug 01 '17 at 15:29
  • ... Seems like Unity fights anything that's even a bit related to OOP :/ I'll have to write a _for_save_only_ class then – agiro Aug 01 '17 at 15:31
  • I completely removed my interfaces and set classes inside alongside all my getters and setters in the class that's inside the `Serializable` class but still no luck. Is that a problem that the class I wish to serialize is a `ScriptableObject`? – agiro Aug 01 '17 at 15:45
  • @Programmer I updated the question but still no luck :/ – agiro Aug 01 '17 at 15:53
  • 1
    Idk. Just try to serialize it without inheriting from `ScriptableObject`. By the way, it is very important to note that you **must** put each class you want to serialize in each **file**. For example, `LevelData` should be put in a file named `LevelData.cs` and `Statistics` should be put in a file called `Statistics.cs`. Unity won't let you serialize it without doing this. – Programmer Aug 01 '17 at 15:54
  • Those are good hints. Yes, I did put all the classes on separate files (1 class 1 file) for those, and I removed the `ScriptableObject` inheritance but still no luck :( Do you think I should try XML or JSON? – agiro Aug 01 '17 at 16:04
  • See [saving data wit json](https://stackoverflow.com/questions/40965645/what-is-the-best-way-to-save-game-state/40966346#40966346). It also talked about this problem you are having and why `BinaryFormatter` should not be used. – Programmer Aug 01 '17 at 17:05
  • True that I found this BinaryFormatter in a Unity tutorial from 2014 :/ I should have been more picky. – agiro Aug 01 '17 at 17:09
  • @Programmer I just got back to try json. But before I start, can I save interfaces and auto props with json? I looked for it but didn't find anything that would indicate it shouldn't. – agiro Aug 01 '17 at 18:16
  • 1
    You can't save auto props with json. I haven't tried with interfaces so you have to try that yourself. – Programmer Aug 01 '17 at 18:20
  • Well a prop in an interface looks like e.g. `float MyProp {get;set;}` so I think that won't do. Or I just use fields in that iface. Thanks again for the help! – agiro Aug 01 '17 at 18:21
  • 2
    Just remove the `{get;set;}`. – Programmer Aug 01 '17 at 18:33
  • @Programmer thought I let you know that your answer regarding `json` worked flawlessly (I used the class as I didn't have the patience to re-write the interface I deleted). If you wish to write an answer so I can accept/upvote please do so. – agiro Aug 01 '17 at 19:22
  • 1
    Glad I was able to help. It's ok because if I put an answer it won't really answer the question in the title of this question. Happy coding! – Programmer Aug 02 '17 at 00:35
  • @Programmer Thats how its fixed for me after removing get and set. thanks:)) – VectorX Feb 25 '19 at 16:21

0 Answers0