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 /
avatar image
0
Question by Wasplay · Sep 25, 2018 at 05:31 PM · serializationsavingsave data

Loading a serialized file after changing the adding a new var to it

Hi,

I'm loading all the data of my game using

             SaveVariables.current = (SaveVariables)bf.Deserialize(file);

But I have to add a new variable to SaveVariables, like that :

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class SaveVariables
 {
     public static SaveVariables current;
 
     public int c = 500;
 
     public bool[] m = new bool[4];
     public bool[] md = new bool[4];
 
     public bool[] p = new bool[5];
     public bool[] pd = new bool[5];
 
     public int roundsPlayed = 0;
     public int gamesPlayed = 0;
     public int bulletsShot = 0;
     public int grenadesShot = 0;
     public int lazersShot = 0;
     public int bouncyShot = 0;
     public int playersDead = 0;
     public int powerupsDestroyed = 0;
 
     public bool sound = false;
     public int ad = 0;
     public string adTime = "";
     public bool rated = false;
     public string newVar = ""; // NEW VARIABLE
 }

After adding the new var, I'm getting an error because an error because this newVar don't exists in the save file How can I fix that ?

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 Wasplay · Sep 26, 2018 at 10:35 AM 0
Share

bump bump bump

avatar image SkaredCreations · Sep 26, 2018 at 03:19 PM 0
Share

I created a class with few public members and used BinaryFormatter to save an instance of this class into a file, then added some more public members to the class and used BinaryFormatter's Deserialize without any issue (the members missing from file were assigned with the default value of their type [of course not with the "default" you set in the class], for example a string member was set to an empty string).

Can you paste here the exception you're getting? Because it's strange that deserialization fails with only missing members (you should have issues if you change or delete a member that is ins$$anonymous$$d stored in the file).

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by hexagonius · Sep 27, 2018 at 08:43 AM

Check out the OptionalFieldAttribute. it's basically what you need. it lets you change the class, adding in new fields marked with this attribute so they will be defaulted on deserialization of data that do not contain them, not throwing errors.
https://docs.microsoft.com/en-us/dotnet/standard/serialization/version-tolerant-serialization#tolerance-of-missing-data

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
avatar image
0

Answer by DownER149 · Sep 26, 2018 at 03:54 PM

i doubt its possible. just output your files as plan text until your ready to publish

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

96 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

Related Questions

Saving system tutorial / Load through main menu 1 Answer

Saving data through FileStream DataInfo? Think i'm misunderstanding something. 1 Answer

When attempting to serialize and save a jagged array it gives me an error message 1 Answer

How To Save User Created Custom Object 1 Answer

Serialized data show information after load 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