- Home /
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!
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 ^^-
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
Your answer
Follow this Question
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