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
2
Question by BugFighter · Nov 06, 2015 at 04:40 PM · c#serializationbinarystatic variable

Deserialize a static class C#

I recently wrote a script that was supposed to store variables for my whole game and also to save and load these via binary formatted serialization. A few days ago, my script had an InvalidCastException but the problem got solved. Now I edited it, but it gives me an error:

`data': cannot declare variables of static types

This is what I changed the script to, now:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic; 
 using System.Runtime.Serialization.Formatters.Binary; 
 using System.IO;
 
 [System.Serializable]
 public static class SaveValues
 {
     public static int highscore = 0;        //saved Highscore
     public static int collectedPoints = 0;    //Points collected on this account
 }
 
 [System.Serializable]
 public static class DataHandleClass
 {
     public static void Load()
     {
         if(File.Exists(Application.persistentDataPath + "/savedGames.bfp"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/savedGames.bfp", FileMode.Open);
             SaveValues data = (SaveValues)bf.Deserialize(file);
             file.Close();
             SaveValues.highscore = data.highscore;
             SaveValues.collectedPoints = data.collectedPoints;
         }
     }
 
     public static void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create (Application.persistentDataPath + "/savedGames.bfp");
 
         SaveValues data = new SaveValues();  //this is the problem
         data.highscore = SaveValues.highscore;
         data.collectedPoints = SaveValues.collectedPoints;
         file.Close();
     }
 }

I get that I cannot declare this static function right there, but I need to have these variables and the class as static so I can easily change them from other scripts. Can anyone help me to correct this? How can I use serialization the right way? Maybe @Bunny83 can help me out again?

Thanks in advance

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

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

Answer by Bunny83 · Nov 07, 2015 at 06:35 AM

Static variables can't be serialized / deserialized, at all. When you deserialize something you get an instance back. Static variables don't belong to an instance.

You should always avoid static variables if possible.

Static variables can raise a lot of problems. A static class can't be instantiated. That's one point of static classes.

If you really want to keep your data static, as i said in my answer on your other question, either use a "singleton" approach (like shown in the other answer) or you have to copy your variables into an actual instance of a class. You could turn your "SaveValues" class into a normal class and keep your static variables. When you want to serialize the values you have to copy the static variables to the instance variables of a temporary instance which you can serialize. When you want to deserialize you do the reverse:

 [System.Serializable]
 public class SaveValues
 {
     public static int highscore = 0;
     public static int collectedPoints = 0;
 
     public int save_highscore = 0;
     public int save_collectedPoints = 0;
 }
 
 public static class DataHandleClass
 {
     public static void Load()
     {
         if(!File.Exists(Application.persistentDataPath + "/savedGames.bfp"))
             return;
         BinaryFormatter bf = new BinaryFormatter();
         using(FileStream file = File.Open(Application.persistentDataPath + "/savedGames.bfp", FileMode.Open))
         {
             SaveValues data = (SaveValues)bf.Deserialize(file);
             file.Close();
             SaveValues.highscore = data.save_highscore;
             SaveValues.collectedPoints = data.save_collectedPoints;
         }
     }
     public static void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         using(FileStream file = File.Create (Application.persistentDataPath + "/savedGames.bfp"))
         {
             SaveValues data = new SaveValues();
             data.save_highscore = SaveValues.highscore;
             data.save_collectedPoints = SaveValues.collectedPoints;
             bf.Serialize(file, data);
             file.Close();
         }
     }
 }

If you don't want to mix instance and static variables in the same class you can use a seperate class which you actually use to serialize the 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Serialization] Multiple classes into one file? 1 Answer

C# how to setup a Binary Serialization 4 Answers

Multiple Cars not working 1 Answer

c# - Function saved through Binary, then XML, then loaded 1 Answer

Serial Exemption when saving binary file 1 Answer


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