Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 26, 2020 at 01:58 PM by mjonasz for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mjonasz · Apr 23, 2020 at 06:24 PM · deserialization

Serialization failed.

I used the following code to save and then load data. The save works well and the highScore ends up in savedGameData:

 {
             savedGameData.highScore = gameController.GetComponent<GameController>().highScore;
 }
 {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Create(Application.persistentDataPath + "/saveGame.sav");
             bf.Serialize(file, savedGameData);
             file.Close();
 }

The load part does not because after deserialization, highScore is 0. I confirmed that it is also 0 in savedGameData when I attached the code to Unity.

 if (File.Exists(Application.persistentDataPath + "/saveGame.sav"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/saveGame.sav", FileMode.Open);
             GameStats savedGameData = (GameStats)bf.Deserialize(file);
             file.Close();
         }

The code also includes:

     public GameStats GetSavedGameData()
     {
         return savedGameData;
     }
 
 }
 [System.Serializable]
 public class GameStats 
 {
     public float highScore;
 }


What did I do wrong here?

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Apr 24, 2020 at 05:43 AM

Well, Are you actually sure about what value you saved? Can you share your "saveGame.sav" file?


Once again I highly recommend to not use the BinaryFormatter for such things. The generated file for such trivial data has huge overhead. Most who choose the BinaryFormatter just want a "non-readable" / not modifiable save. Both is no achieved that way. It's a little bit harder but quite trivial. I highly recommend to just use a human readable format like JSON and if you want to make it non readable, just use a simple "scramble" / "unscramble" once you're ready to ship your game. You could simply use base64 encoding which makes it equally difficult to read / modify. However a simple xor of the data with a fix key would be much more difficult to break than the BinaryFormatter. That way it would make debugging much easier since you can simply disable the obfuscation until you actually ship the game.


The BinaryFormatter uses a well known format (the MS remoting protocol). It's in general not hard to read or modify. It's also quite verbose. But the biggest issue is that it's very strict and doesn't allow any change in the structure of the saved data. So a new version where you changed some fields of your class would completely break older saves.


Regardless if you really want to stick to it you should first make sure what value you actually serialized. Again, if you can share a sample saveGame.sav file I can tell you what value is stored inside.

Comment
Add comment · Show 3 · 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 mjonasz · Apr 24, 2020 at 06:53 PM 0
Share

Hi,

I am sending the saveGame file. Now that you mention it, I don't think it saved anything because when I opened it with notepad I did not see any numbers that would represent the highScore. But I do know that the highScore did get written into SaveGame because I saw it there when debugging. So this means that something is wrong with

  {
              savedGameData.highScore = gameController.GetComponent<GameController>().highScore;
  }
  {
              BinaryFormatter bf = new BinaryFormatter();
              FileStream file = File.Create(Application.persistentDataPath + "/saveGame.sav");
              bf.Serialize(file, savedGameData);
              file.Close();
  }

link text

savegame.zip (238 B)
avatar image Bunny83 mjonasz · Apr 24, 2020 at 11:18 PM 0
Share

Well, as far as I can tell the highscore in the file is 300 if I'm not mistaken ^^ Though I just quickly glanced over it with a hex editor. What do you actually do with your deserialited value? In all the code you've posted so far so don't do anything with the deserialized GameStats instance. Is it possible that you have declared a variable called "savedGameData" inside your class and you just hide that variable through your local variable? So this line:

 GameStats savedGameData = (GameStats)bf.Deserialize(file);

should be this line:

 savedGameData = (GameStats)bf.Deserialize(file);

Though that's hard to tell since we don't see your whole script.


edit
Here's the essential part of your save file in hex and where the actual value is stored

 // 05 01000000 0947616D655374617473 01000000
 // |  |        |                    |
 // |  |        |                    +- member count == 1
 // |  |        +- length prefixed type name(9 characters) == "GameStats"
 // |  +- Object ID == 1
 // +- record type 5 == Class with member and types
 
 
 // 096869676853636F7265 00 0B 02000000 00009643 0B
 // |                    |  |  |        |        +- End ot stream
 // |                    |  |  |        +- actual float value 0x43960000 == 300
 // |                    |  |  +- library ID (was defined earlier; essentially
 // |                    |  |                 references "Assembly-CSharp")
 // |                    |  +- primitive type enum (11 == float)
 // |                    +- member type (0 == primitive type)
 // +- length prefixed member name (9 characters) == "highScore"

As I said, well documented and relatively easy to read, even without knowing anything about the code. The format is self describing. The whole definition of the class and it's members is in the data.

avatar image mjonasz Bunny83 · Apr 24, 2020 at 11:48 PM 0
Share

I made the correction you indicated and it works. To answer your question, highScore gets deserialized on Start and then goes to the highScore Text display.
Big thanks!

Follow this Question

Answers Answers and Comments

125 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

Related Questions

Deserialize Facebook friends result 1 Answer

Deserializing Json problem in Hololens app 1 Answer

Save data through xml Serialization on iOS 1 Answer

Serialization creates file while returning nothing 2 Answers

How to serialize/deserialize entities at runtime -1 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