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 Mojova · Sep 28, 2014 at 03:14 PM · serializationloadingsaving

How to deserialize after I've added more members to a class

I have been struggling with saving my game progress. I have a class called Game that I serialize to a file. But every time I add another member to Game class I am getting a serialization exception when trying to deserialize a version of the Game class that doesn't have the new member. What I have done now when getting the serialization exception I delete the old file, create a new save and lose all the progress. Is there any way to deserialize a older version of the class into a newer version with the added members?

This is the save class I am using

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public static class SaveLoad {    
 
     public static Game current;
 
     private static string fileName = Application.persistentDataPath + "/progress.hamburger";    //Name of the file to save to
 
     /// <summary>
     /// Creates a new game and saves it to a file
     /// </summary>
     public static void CreateGame() {
         current = new Game();
         Save();
     }
 
     /// <summary>
     /// Saves the progress on levels and updates the inventory of the current game with players inventory
     /// </summary>
     public static void Save() {
         if (current.unlockedLevels.Length != LevelSelect.instance.levels.Length)
         {
             current = new Game();
         }
         for (int i = 0; i < current.unlockedLevels.Length; i++) {
             current.unlockedLevels[i] = LevelSelect.instance.levels[i].unlocked;
             current.highestCombos[i] = LevelSelect.instance.levels[i].highestCombo;
             current.bestScores[i] = LevelSelect.instance.levels[i].bestScore;
         }
         current.musicMuted = MusicHandler.instance.muted;
         current.soundMuted = SoundHandler.instance.muted;
         current.inventory.UpdateInventory(Initializer.instance.player.inventory);
         BinaryFormatter bf = new BinaryFormatter();
         
         FileStream file = File.Create(fileName);
         bf.Serialize(file, SaveLoad.current);
         file.Close();
     }
 
     /// <summary>
     /// Loads the progress from file if the files exists
     /// otherwise it creates a game and the file
     /// </summary>
     public static void Load() {        
         if (File.Exists(fileName)) {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(fileName, FileMode.Open);            
             try {
                 SaveLoad.current = (Game)bf.Deserialize(file);
             } catch (System.Runtime.Serialization.SerializationException e) {
                 if (e != null) {
                     file.Close();
                     CreateGame();
                 }
             }
             file.Close();
             if (MusicHandler.instance.muted != current.musicMuted)
                 MusicHandler.instance.Mute();
             if (SoundHandler.instance.muted != current.soundMuted)
                 SoundHandler.instance.Mute();
             Initializer.instance.player.inventory.UpdateInventory(current.inventory);
             for (int i = 0; i < current.unlockedLevels.Length; i++) {
                 LevelSelect.instance.levels[i].unlocked = current.unlockedLevels[i];
                 LevelSelect.instance.levels[i].highestCombo = current.highestCombos[i];
                 LevelSelect.instance.levels[i].bestScore = current.bestScores[i];
             }            
             return;
         }
         else
             CreateGame();        
     }
     
 }
 
 //Contains all stuff needed to save
 [System.Serializable]
 public class Game {
     
     public bool[] unlockedLevels;
     public int[] highestCombos;
     public int[] bestScores;
     public Inventory inventory;
 
     public bool soundMuted = false;
     public bool musicMuted = false;
 
     public Game() {
         unlockedLevels = new bool[LevelSelect.instance.levels.Length];
         highestCombos = new int[LevelSelect.instance.levels.Length];
         bestScores = new int[LevelSelect.instance.levels.Length];
 
         for (int i = 0; i < unlockedLevels.Length; i++) {
             unlockedLevels[i] = LevelSelect.instance.levels[i].unlocked;            
             highestCombos[i] = LevelSelect.instance.levels[i].highestCombo;
             bestScores[i] = LevelSelect.instance.levels[i].bestScore;            
         }
         inventory = new Inventory();
     }
 }
Comment
Add comment · Show 1
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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Save/Load Animation State of Instantiated Prefabs 0 Answers

Serialization Location 1 Answer

Serializing a graph 2 Answers

how to properly save an int value ? 2 Answers

I am getting a serialization exception error when trying to save and load in Unity? 2 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