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 /
  • Help Room /
avatar image
0
Question by Viaticum · Jun 09, 2016 at 05:58 AM · serialization

Binary Serialization problem.

Hello everyone!

I'm having a little binary serialization problem and I hope someone can help. Using the following code, when I run my Save() function, it appears to run just fine and creates the proper file. When I run the Load() function, it runs but my Debug.Log I have set up in another script that directly reads the GameData.xxxx variables, all return their default/null values.

I should also add, if I run the game, alter all the variables, hit my "test save" (which applies all entry boxes / dropdown to GameData's static variables), then hit "test load" which calls the Load() function (file names for both save and load are the same, verified), it does work! It is just when I set up a character, hit save, exit, start the game again, hit load, it recalls only default values.

Here is my GameManager script with accompanying GameData. Any advice would be much appreciated!

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GameManager : MonoBehaviour {
 
     public static GameManager game;
 
     private readonly string fileSuffix = ".vgs"; //Viaticum Game Save
 
     void Awake () {
         if(!game){
             DontDestroyOnLoad(gameObject);
             game = this;
         } else {
             Destroy(gameObject);
         }
     }
 
     public void Save(string fileName){
         fileName = Application.persistentDataPath + "/" + fileName + fileSuffix;
         Debug.Log("Save: " + fileName);
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Open(fileName, FileMode.OpenOrCreate);
         bf.Serialize(file, GameData.data);
         file.Close();
     }
 
     public void Load(string fileName){
         fileName = Application.persistentDataPath + "/" + fileName + fileSuffix;
         if (File.Exists(fileName)){
             Debug.Log("Load: " + fileName);
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(fileName, FileMode.Open);
             GameData.data = (GameData)bf.Deserialize(file);
         } else {
             Debug.Log("File not found: " + fileName);
         }
     }
 }
 
 [Serializable]
 public class GameData
 {
     public static GameData data = new GameData();
 
     public static string playerName;
     public static int playerSex;
     public static int playerClass;
     public static int playerElement;
     public static int playerSpecialty;
     public static int playerLineage;
     public static int playerHomeland;
     public static int playerSkill;
     public static int playerSpell;
     public static int playerItem;
 }
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 robnwfoo · Jun 09, 2016 at 11:30 AM 0
Share

Have you tried putting the GameData constructor somewhere else (perhaps where you set the variables)?

Seems kind of funky to construct an object within it's type's blueprints before any of the members get initialized. I mean just off the top of my head something like that wouldn't even work in C/++, but I don't know the finer details of the CLR for situations like this.

avatar image Viaticum robnwfoo · Jun 17, 2016 at 08:48 AM 0
Share

Sorry for the delayed response, have been pulled away for a while due to unexpected circumstances.

I have yet to try that but you might be right, or at least on the right track! I'll let you know if it works, or what did work when I eventually figure it out. I will bang my head against this rock until it turns to dust! Haha.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JedBeryll · Jun 17, 2016 at 09:18 PM

I don't think you can serialize static variables. Loading after saving works because the static variables don't change. However when you load after restarting, you actually don't load anything so no changes in values. If you already have a static reference to a GameData, you can access any variables of the class. If you want this to work you have to make the variables non-static.

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

61 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

Related Questions

[CLOSED]Problem with serialization after PUN update 2 Answers

Cannot read past end of stream, MemoryStream 1 Answer

TextAsset thinks file is ~6856 bytes when it is 120123 bytes 0 Answers

Cant save/load game data 2 Answers

How to serialize a List of ContentPacks (from Morph3D) in Photon Unity Networking? 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