Why has my BinaryFormatter stopped working?

Hi,

For some reason I am getting “SerializationException: Unexpected binary element: 255” when I build my game for iOS. It has been working fine for the past couple of weeks, and nothing has been changed in the code, but today when I build it, I always get this exception.

On a seemingly unrelated note, I have just added the Facebook SDK and OpenKit plugin. I also took out the extra from the plist.

If you need any more information, I would be happy to provide you with it.

Thanks,

BPF


Here is the code for the BinaryFormatter:

if (File.Exists(Application.persistentDataPath + "/savedData.dat"))
{
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(Application.persistentDataPath + "/savedData.dat", FileMode.Open,FileAccess.Read,FileShare.None);
    SaveData data = (SaveData)bf.Deserialize(file);
    file.Close();
    achievements = data.achievements;        // bool[]
    pebbleType = data.pebbleType;            // int
    charType = data.charType;                // int
    pebbleOverlayR = data.pebbleOverlayR;    // float
    pebbleOverlayG = data.pebbleOverlayG;    // float
    pebbleOverlayB = data.pebbleOverlayB;    // float
    pebbleColliders = data.pebbleColliders;  // CollisionPath
    highScore = data.highScore;              // float
    soundOn = data.soundOn;                  // bool
    musicOn = data.musicOn;                  // bool
}

You must add this line in the Awake method in the class your are using the Serializer:

Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");

If you try adding the code solution and think it didn’t help, you MUST uninstall the app from your device.

That’s because of the data corruption on the file you attempt to create in the first place.

I had the issue, and that solve the problem.

You can get more information from:

-FileStream + BinaryFormatter from C# to iOS doesn't work? - Unity Answers

-Why did my BinarySerialzer stop working? - Unity Answers

I have experienced the same kind of issue and the code didn’t worked

Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");

The solution for me was simple. I had to separate the part that of the script that used the BinaryFormater into its own script. To put it simple, I have a separate C# script named “Database.cs” and it has all the save and load functions set as static. Don’t even need to put that script on any GameObject. I got a GameObject (GameSystem) with DontDestroy() that has the “current game” public static getters and setters which is created when the game is launched. The “Database.cs” script read specific binary files and set the data it read into the static getters and setters.

I noticed that, if I put the BinaryFormater script directly into the GameSystem GameObject, it returns the Unexpected Binary Element 255 error. If I keep it as is, but set it into its own static script, it works as intended. I also noticed that if I bruteforce the game to keep on even with the Unexpected Binary Element 255 error, I get loads of errors telling me that the data from the BinaryFormater is now corrupted.

This is just a guess of mine, but I would think that having the BinaryFormater within an active GameObject in a scene ends up corrupting the data used while the BinaryFormater has some task to complete. By separating it into its own script, it’s now in its own memory block and doesn’t get corrupted.

This guess of mine comes from the fact that the exact same code works if it’s in its own C# script file while return the error 255 if in the script of an active GameObject in the scene.