Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BPF2014 · Jul 30, 2014 at 10:52 AM · serializationfacebookexceptionbinaryformatter

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
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Jul 30, 2014 at 11:02 AM 0
Share

has your SaveData structure changed?

EDIT: which line is your error on?

avatar image BPF2014 · Jul 30, 2014 at 11:27 AM 0
Share

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.

avatar image gjf · Jul 30, 2014 at 11:54 AM 0
Share

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...

avatar image BPF2014 · Jul 30, 2014 at 12:06 PM 0
Share

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();

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Crillst · Mar 03, 2015 at 08:01 PM 0
Share

Thanks, this works well.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

EndOfStreamException : Failed to read past end of stream 1 Answer

What's a fast serializer that works cross platform that can be used for saving/loading custom levels? 1 Answer

EndOfStreamException : Failed to read past end of stream 1 Answer

Save multiple scriptableObjects in just one binary file 2 Answers

How to Serialize Scriptable Object at Runtime? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges