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 Abacab · Jun 25, 2015 at 09:36 PM · c#saveloadfilestream

Filestream/SaveManager

Hello!

I created a save/load script by following a guide on youtube, but I get this error when SaveLoad.Load(); is called:

System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) [0x00000] in :0 at SaveLoad.Save () [0x00000] in :0 (Filename: Line: -1)

 public static void Save ()
     {
         BinaryFormatter binary = new BinaryFormatter ();
         FileStream fStream = File.Create (Application.persistentDataPath + "/turbocoinSaveFile.sav");
 
         SaveManager sManager = new SaveManager ();
         sManager.coins = CoinController.coins;
         sManager.coinsTotalEver = CoinController.coinsTotalEver;
         sManager.totalTurbosEver = Statistics.totalTurbosUsed;
 
         binary.Serialize (fStream, sManager);
         fStream.Close ();
     }
 
     public static void Load ()
     {
         if (File.Exists (Application.persistentDataPath + "/turbocoinSaveFile.sav")) {
             BinaryFormatter binary = new BinaryFormatter ();
             FileStream fStream = File.Open (Application.persistentDataPath + "/turbocoinSaveFile.sav", FileMode.Open);
             SaveManager sManager = (SaveManager)binary.Deserialize (fStream);
             fStream.Close ();
 
             CoinController.coins = sManager.coins;
             CoinController.coinsTotalEver = sManager.coinsTotalEver;
             Statistics.totalTurbosUsed = sManager.totalTurbosEver;
         }
     }

  [Serializable]
  class SaveManager
  {
      public int coins;
      public int coinsTotalEver;
      public int totalTurbosEver;

}

Comment
Add comment · Show 4
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 Clint.Carpenter · Jun 26, 2015 at 12:41 AM 0
Share

The error you copied seems to reference the SaveLoad.Save () method. Also can you include your definition for Save$$anonymous$$anager? Are they all simple types in it?

avatar image Abacab · Jun 26, 2015 at 12:42 AM 0
Share
 [Serializable]
 class Save$$anonymous$$anager
 {
     public int coinsTotalEver;
     public int totalTurbosEver;
 }
avatar image Cherno · Jun 26, 2015 at 01:20 AM 0
Share

I see no variable called coins in there ?!?

avatar image Abacab · Jun 26, 2015 at 01:22 AM 0
Share

Oh yes there is, I accidentally deleted it from my comment. :)

 [Serializable]
 class Save$$anonymous$$anager
 {
     public int coins;
     public int coinsTotalEver;
     public int totalTurbosEver;
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by kmgr · Jun 26, 2015 at 09:22 AM

Might be an issue with IDisposable interface, as you're not calling Dispose() explicitly. Try wraping I/O stuff in the 'using' clause.

 void Save()
 {
     using(FileStream fs = File.Create("filename"))
     {
          BinaryFormatter binf = new BinaryFormatter();
          binf.Serialize(fs, saveData);
     }
 }
Comment
Add comment · Show 14 · 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 Abacab · Jun 26, 2015 at 09:27 AM 0
Share

How would this look with 'using'?

 public static void Save ()
      {
          BinaryFormatter binary = new BinaryFormatter ();
          FileStream fStream = File.Create (Application.persistentDataPath + "/turbocoinSaveFile.sav");
  
          Save$$anonymous$$anager s$$anonymous$$anager = new Save$$anonymous$$anager ();
          s$$anonymous$$anager.coins = CoinController.coins;
          s$$anonymous$$anager.coinsTotalEver = CoinController.coinsTotalEver;
          s$$anonymous$$anager.totalTurbosEver = Statistics.totalTurbosUsed;
  
          binary.Serialize (fStream, s$$anonymous$$anager);
          fStream.Close ();
      }
avatar image kmgr · Jun 26, 2015 at 09:31 AM 1
Share
 void Save()
  {
      using(FileStream fs = File.Create(Application.persistentDataPath + "/turbocoinSaveFile.sav"))
      {
           BinaryFormatter binf = new BinaryFormatter();
           Save$$anonymous$$anager s$$anonymous$$anager = new Save$$anonymous$$anager ();
           s$$anonymous$$anager.coins = CoinController.coins;
           s$$anonymous$$anager.coinsTotalEver = CoinController.coinsTotalEver;
           s$$anonymous$$anager.totalTurbosEver = Statistics.totalTurbosUsed;
           binf.Serialize(fs, s$$anonymous$$anager);
      }
  }

avatar image Abacab · Jun 26, 2015 at 09:32 AM 0
Share

Thanks! Would the Load()-function look different?

avatar image kmgr · Jun 26, 2015 at 09:33 AM 0
Share

Load() method would only be different in the way that you call Deserialize, but the 'using' stuff looks the same.

avatar image kmgr · Jun 26, 2015 at 09:41 AM 1
Share

I would move File.Exists outside, so that you're first checking if the file exists and the open it. Also in the Load() method you should call File.OpenRead() not File.Create()

 void Load()
 {
     if (File.Exists("Application.persistentDataPath + "/turbocoinSaveFile.sav"))
     {
          using(FileStream fs = File.OpenRead("Application.persistentDataPath + "/turbocoinSaveFile.sav"))
          {
              BinaryFormatter binf = new BinaryFormatter();
              Save$$anonymous$$anager s$$anonymous$$anager = (Save$$anonymous$$anager)binf.Deserialize(fs);
          }
     }
 }
Show more comments
avatar image
0

Answer by Clint.Carpenter · Jun 26, 2015 at 05:19 PM

Looking again it seems like the error is in your save function specifically with your file. Check that fileStream != null after you create it.

Are you running this code from the editor? you are not by chance trying to run this from a web application are you? Because then the persistentDataPath would not work.

Comment
Add comment · Show 5 · 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 Abacab · Jun 26, 2015 at 05:24 PM 0
Share

$$anonymous$$obile! :) What is wrong with the file?

avatar image Clint.Carpenter · Jun 26, 2015 at 06:29 PM 0
Share

There goes that theory.

Last thing I can think to try is to hard code the values you are putting into Save$$anonymous$$anager just to make sure it isn't a error with the data (like null or uninitialized)

avatar image Abacab · Jun 26, 2015 at 06:31 PM 0
Share

what if I added if (!= null) to each value? Would it work?

For example:

 if (CoinController.coins != null) {
             s$$anonymous$$anager.coins = CoinController.coins;
         }
avatar image Clint.Carpenter · Jun 26, 2015 at 08:46 PM 0
Share

I am shooting in the dark here, but I would try:

s$$anonymous$$anager.coins = 2; s$$anonymous$$anager.coinsTotalEver = 3; s$$anonymous$$anager.totalTurbosEver = 4;

Then at least you can be certain the data is or IS NOT part of the problem.

avatar image Abacab · Jun 28, 2015 at 09:59 PM 0
Share

Not working with:

 public static void Save ()
     {
         BinaryFormatter binary = new BinaryFormatter ();
         FileStream fStream = File.Create (Application.persistentDataPath + "/playerData.sav");
 
         Save$$anonymous$$anager s$$anonymous$$anager = new Save$$anonymous$$anager ();
         if (PlayerPrefs.Has$$anonymous$$ey ("Coins")) {
             s$$anonymous$$anager.coins = CoinController.coins;
         }
         if (PlayerPrefs.Has$$anonymous$$ey ("TotalCoinsEver")) {
             s$$anonymous$$anager.coinsTotalEver = CoinController.coinsTotalEver;
         }
         if (PlayerPrefs.Has$$anonymous$$ey ("TotalTurbosUsed")) {
             s$$anonymous$$anager.totalTurbosEver = Statistics.totalTurbosUsed;
         }
 
         binary.Serialize (fStream, s$$anonymous$$anager);
         fStream.Close ();
     }

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

24 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

Related Questions

Coding help: How to use xml serialization 1 Answer

The name "Game Foundation" does not exist in the current context 1 Answer

Multiple Cars not working 1 Answer

How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers

[Solved] Built in GUI continuously Loads through different Options. 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