Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Skeasy · Jan 24, 2020 at 02:40 PM · fpsexceptionserializeserializable

SerializationException: End of Stream encountered before parsing was completed

Hello everybody,

hours of research and debugging couldn't help me, which is why you are my last hope. The error "SerializationException: End of Stream encountered before parsing was completed" occurs when I try to read a file from a local folder.

I already tried:

  • using "using" instead of just stream.Close();

  • setting stream.position = 0

  • searching for errors like serialized class not serializable or other syntax errors

The main class to save:

 using UnityEngine;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public static class SaveHandler
 {
     private static string filePath = Application.persistentDataPath + "/2401202.sav";
     public static void save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate))
         {
             //fs.Position = 0;
             SaveDataCollector saveData = new SaveDataCollector();
             bf.Serialize(fs, saveData);
         }
     }
 
     public static void load()
     {
         if (File.Exists(filePath))
         {
             BinaryFormatter bif = new BinaryFormatter();
             using (FileStream fes = new FileStream(filePath, FileMode.Open))
             {
                 //fes.Position = 0;
                 SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
                 savoDato.distributeData();
             }
         }
         else
         {
             Debug.Log("Errorcode 401: Savefile not Found");
         }
     }
 }
 
     public static void load()
     {
         if (File.Exists(filePath))
         {
             BinaryFormatter bif = new BinaryFormatter();
             using (FileStream fes = new FileStream(filePath, FileMode.Open))
             {
                 //fes.Position = 0;
                 SaveDataCollector savoDato = bif.Deserialize(fes) as SaveDataCollector;
                 savoDato.distributeData();
             }
         }
         else
         {
             Debug.Log("Errorcode 401: Savefile not Found");
         }
     }

The serialized class:

 using UnityEngine;
 
 [System.Serializable]
 public class SaveDataCollector
 {
     public float highScore;
     public bool legacyControl;
 
     public SaveDataCollector()
     {
         legacyControl = InterData.legacyControl;
         highScore = InterData.highScore;
     }
 
     public void distributeData()
     {
         InterData.legacyControl = this.legacyControl;
         InterData.highScore = this.highScore;
     }
 }

Please help me, I'm really getting desperate. Thanks in advance!

Comment
Add comment · Show 2
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 Skeasy · Jan 24, 2020 at 02:44 PM 0
Share

why is this code formatter not working? wtf

avatar image Neran28 · Jan 24, 2020 at 03:26 PM 0
Share

Cannot give you a more precise answer right now but this error might occur if you changed your savedatacollector structure and are trying to deserialize a file with the old data structure.

1 Reply

· Add your reply
  • Sort: 
avatar image
6

Answer by Bunny83 · Jan 24, 2020 at 05:24 PM

You most likely try to load invalid or outdated data. Do you have modified your "SaveDataCollector" after you saved the file? This would completely break the file you saved. That's the main reason why the BinaryFormatter is a bad choice for a savegame solution apart from the large amount of overhead.


Have you tried actually deleting your save file and saving a new one? Have you checked if the file was actually created? Note that your save class can only store a single float and a single bool value at the moment.

Comment
Add comment · Show 4 · 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 Skeasy · Jan 24, 2020 at 07:29 PM 0
Share

Hi, first off, thank you very much for your answer.

Yes, the file has been created and I deleted and created new ones multiple times with different names. And yes my save class isnt generic at all, but its okay for my needs and I am already researching OR$$anonymous$$.

You said, that BinaryFormatter isn't the ideal choice. I learned it from Brackeyes YT Channel. What method would you suggest?

Sincerely, Skeasy

avatar image Bunny83 Skeasy · Jan 25, 2020 at 01:12 AM 0
Share

The BinaryFormatter uses $$anonymous$$icrosoft's Remoting protocol. It's a verbose binary format. However it is also quite intolerant to any kind of changes to the structure. Also note that your approach is quite dangerous. The constructor of your SaveDataCollector directly reads static fields on another class. You really shouldn't do that. Whenever an instance is created the constructor will be called.


Better data format choices are common flexible formats like JSON, X$$anonymous$$L or you could roll your own binary format which would be way smaller and you have full control over the content. Of course to actually create a binary format that doesn't have the same issues as the BinaryFormatter you have to take care of deciding what format you deal with. Almost all file formats contain some sort of version inside the file header. This ensures that you know for sure what data is actually stored in the file.


If you want to use JSON you can either use Unity's JsonUtility or any other JSON framework like my SimpleJSON framework for example. Unity's JsonUtility is also an object mapper. So you have to create serializable classes which Unity can serialize. However the serializer is a lot more tolerant and also allows partial overwriting. $$anonymous$$y SimpleJSON does not map the data to fix predefined classes but just parses the JSON and provide an easy interface to work with the data.


If you want to use a binary format, the easiest way is to use the BinaryWriter / BinaryReader. If you want to see an example use, have a look at my custom $$anonymous$$eshSerializer which can serialize any Unity $$anonymous$$esh into a custom binary format.

avatar image Macarona_Virus · Jun 30, 2020 at 07:16 AM 0
Share

THANKS!!!! THANKS !!!!!!!

avatar image jacobrutter · Mar 06 at 09:46 PM 0
Share

This was exactly the scenario in my case. Thanks for the answer!

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

166 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Type UnityEngine.GameObject is not marked as Serializable. 1 Answer

Keeping information on custom Inspector window using List of List 1 Answer

Need to serialize a Monobehaviour Class 2 Answers

Non-static Singleton implementation 1 Answer

Saving Non-Prefab Data During Editor Play 0 Answers


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