- Home /
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
}
has your SaveData
structure changed?
EDIT: which line is your error on?
No, the SaveData is still the same. In the xCode logs, it seems like it cannot find the file; it says "(Filename: Line: -1)" I know the save file exists, because it has been working before, and nothing has been changed on the device.
what about your serialization code?
i've seen (non-unity) issues in the past where serialization creates a corrupt file - some type not being handled correctly.
maybe the addition of fb/etc. has subtley changed something...
As far as I'm aware, the file has not changed, and the serialization code is still the same:
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/savedData.dat");
SaveData data = new SaveData();
data.achievements = achievements;
data.pebbleType = pebbleType;
data.charType = charType;
data.pebbleOverlayR = pebbleOverlayR;
data.pebbleOverlayG = pebbleOverlayG;
data.pebbleOverlayB = pebbleOverlayB;
data.pebbleColliders = pebbleColliders;
data.highScore = highScore;
data.soundOn = soundOn;
data.musicOn = musicOn;
bf.Serialize(file, data);
file.Close();
Answer by WingSoft · Jan 28, 2015 at 05:35 PM
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:
-http://answers.unity3d.com/questions/725419/filestream-binaryformatter-from-c-to-ios-doesnt-wo.html
-http://answers.unity3d.com/questions/30930/why-did-my-binaryserialzer-stop-working.html
Answer by Max_Bol · Nov 09, 2018 at 05:44 PM
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.