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 MingHin0000 · Dec 16, 2016 at 11:55 AM · c#unity 5scripting problemerrorbutton

Why there is an 'SerializationException: serializationStream supports seeking, but its length is 0' error when I click the load button?

I am creating a clicker game but I get stuck in the SaveLoad script. I tried to run the game. When I click the save button, it saves a data into my computer. But when I click the load button, the following error occurs:

  SerializationException: serializationStream supports seeking, but its length is 0
     System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:155)
     System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136)
     SaveLoad.Load () (at Assets/Script/SaveLoad.cs:26)
     StatsManager.Load () (at Assets/Script/StatsManager.cs:23)
     UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
     UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634)
     UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769)
     UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
     UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
     UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
     UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
     UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
     UnityEngine.EventSystems.EventSystem:Update()
 

My SaveLoad code is here:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public static class SaveLoad
 {
 
     public static void Save(StatsManager stats) {
 
         BinaryFormatter bf = new BinaryFormatter();
         FileStream stream = new FileStream(Application.persistentDataPath + "/playerData.sav", FileMode.Create);
 
         PlayerData data = new PlayerData(stats);
 
         bf.Serialize(stream, data);
         stream.Close();
     }
 
     public static float[] Load() {
         if (File.Exists(Application.persistentDataPath + "/playerData.dat")){
             BinaryFormatter bf = new BinaryFormatter();
             FileStream stream = new FileStream(Application.persistentDataPath + "/playerData.dat", FileMode.Open);
 
             PlayerData data = bf.Deserialize(stream) as PlayerData;
             stream.Close();
 
             return data.gameStats;
         }
         else {
             Debug.LogError("File does not exist!");
             return new float[4];
         }
     
     }
 }
 
 [System.Serializable]
 public class PlayerData {
 
     public float[] gameStats;
 
     public PlayerData(StatsManager stats) {
         gameStats = new float[4];
         gameStats[0] = stats.cash;
         gameStats[1] = stats.cpc;
         gameStats[2] = stats.resetBonus;
         gameStats[3] = stats.resetPower;
     }
 }

And the StatsManager code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class StatsManager : MonoBehaviour {
     public float cash;
     public float cpc = 1;
     public float resetBonus = 1;
     public float resetPower;
     public Click click;
 
     void Update() {
         click.CashDisplay.text = "$" + CurrencyConverter.Instance.GetCurrencyIntoString(cash, false, false);
         click.cpc.text = "$" + CurrencyConverter.Instance.GetCurrencyIntoString(cpc, false, true);
         click.PowerDisplay.text = "Prestige Power: " + CurrencyConverter.Instance.GetCurrencyIntoString(resetPower, false, false) + "\n(x" + CurrencyConverter.Instance.GetCurrencyIntoString(resetBonus, false, false) + " Multiplier)";
     }
 
     public void Save() {
         SaveLoad.Save(this);
     }
 
     public void Load() {
         float[] loadedStats = SaveLoad.Load();
 
         cash = loadedStats[0];
         cpc = loadedStats[1];
         resetBonus = loadedStats[2];
         resetPower = loadedStats[3];
     }
 
 
 }

How can I solve this problem? Thank you!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Dec 21, 2016 at 04:10 AM

Well, the error is pretty clear. The input stream has a length of "0" so your file is empty. Why is it empty? Because "playerData.sav" doesn't equal "playerData.dat". That's why you want to use constants or variables for hardcoded filenames ^^-

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 MingHin0000 · Dec 21, 2016 at 07:36 AM

Problem solved. You see, what the heck is that?! Hint: In SaveLoad line 13 and 22

Comment
Add comment · Show 1 · 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 Bunny83 · Dec 21, 2016 at 11:48 AM 0
Share

Uhm, isn't that what i told you in my answer?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

script does not update inspector? 3 Answers

Unity can't use the script. 0 Answers

Built project, now scripts are missing. 2 Answers

Unity 5 UNet Spawn as Child. 1 Answer

Error CS0029: Cannot implicitly convert type to UnityEngine.UI.Transform 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