- Home /
I am getting a serialization exception error when trying to save and load in Unity?
I am attempting to save and load and it was working. The only thing I changed was attempting to save array but after that i started getting error : SerializationException: serializationStream supports seeking, but its length is 0.
My Code:
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class GameControl : MonoBehaviour {
public Click click;
public GoldPerSec gps;
public GameObject[] upgrades;
public GameObject[] items;
void OnApplicationQuit()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData ();
data.gold = click.gold;
data.goldperclick = click.goldperclick;
data.clickmult = click.clickMult;
data.gemcount = click.gems;
data.gpsmult = gps.gpsMult;
for (int i = 0; i < upgrades.Length; i++) {
data.upgradesName[i] = upgrades[i].GetComponent<UpgradeManager>().itemName;
data.upgradesCount[i] = upgrades[i].GetComponent<UpgradeManager> ().count;
data.upgradesCost[i] = (int)upgrades[i].GetComponent<UpgradeManager> ().cost;
}
for (int i = 0; i < items.Length; i++) {
data.managerName[i] = items[i].GetComponent<ItemManager>().itemName;
data.managerCount[i] = items[i].GetComponent<ItemManager> ().count;
data.managerCost[i] = (int)items[i].GetComponent<ItemManager> ().cost;
}
bf.Serialize (file, data);
file.Close ();
}
void Start()
{
if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize (file);
file.Close ();
click.gold = data.gold;
click.goldperclick = data.goldperclick;
click.clickMult = data.clickmult;
click.gems = (int)data.gemcount;
gps.gpsMult = data.gpsmult;
for (int i = 0; i < upgrades.Length; i++) {
upgrades[i].GetComponent<UpgradeManager>().itemName = data.upgradesName[i];
upgrades[i].GetComponent<UpgradeManager> ().count = data.upgradesCount[i];
upgrades[i].GetComponent<UpgradeManager> ().cost = data.upgradesCost[i];
}
for (int i = 0; i < items.Length; i++) {
items[i].GetComponent<ItemManager>().itemName = data.managerName[i];
items[i].GetComponent<ItemManager> ().count = data.managerCount[i];
items[i].GetComponent<ItemManager> ().cost = data.managerCost[i];
}
}
}
}
[Serializable]
class PlayerData
{
public float gold;
public float goldperclick;
public float clickmult;
public float gemcount;
public float gpsmult;
public string[] upgradesName;
public int[] upgradesCost;
public int[] upgradesCount;
public string[] managerName;
public int[] managerCost;
public int[] managerCount;
}
i usually work with X$$anonymous$$L, and with X$$anonymous$$L serializable you have to mark each property... ID$$anonymous$$ if thats the case with BinaryFormatter's but.... some additional information might help...
what line does the error occur on?
what does the 'Click' class look like?
what does the 'GoldPerSec' class look like?
ps. a quick google found this thread:
http://answers.unity3d.com/questions/767580/serialization-exception-error-when-trying-to-load.html
I saw the thread and i have already called formater.serialize function. but as i was debugging i found that there is some problem when i am trying to copy int value from a script attached to gameobject to data.upgrade.
for (int i = 0; i < upgrades.Length; i++) {
data.upgradesName[i] = upgrades[i].GetComponent<Upgrade$$anonymous$$anager>().itemName;
data.upgradesCount[i] = upgrades[i].GetComponent<Upgrade$$anonymous$$anager> ().count;
data.upgradesCost[i] = (int)upgrades[i].GetComponent<Upgrade$$anonymous$$anager> ().cost;
}
Its only giving me error when i do this.
I believe the issue in that thread was an empty array during serialization. $$anonymous$$aybe 'upgrades' is not being defined (or is empty) at runtime? A Debug.Log() before that loop might yield more information.
Answer by ninja_gear · May 27, 2016 at 06:22 AM
If its not upgrades array then its prolly the arrays inside data. You never set them before u attempt to add to them.
This MIGHT work:
void OnApplicationQuit()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.gold = click.gold;
data.goldperclick = click.goldperclick;
data.clickmult = click.clickMult;
data.gemcount = click.gems;
data.gpsmult = gps.gpsMult;
data.upgradesName = new string[upgrades.Length];
data.upgradesCost = new int[upgrades.Length];
data.upgradesCount = new int[upgrades.Length];
for (int i = 0; i < upgrades.Length; i++)
{
data.upgradesName[i] = upgrades[i].GetComponent<UpgradeManager>().itemName;
data.upgradesCount[i] = upgrades[i].GetComponent<UpgradeManager>().count;
data.upgradesCost[i] = (int)upgrades[i].GetComponent<UpgradeManager>().cost;
}
data.managerName = new string[items.Length];
data.managerCost = new int[items.Length];
data.managerCount = new int[items.Length];
for (int i = 0; i < items.Length; i++)
{
data.managerName[i] = items[i].GetComponent<ItemManager>().itemName;
data.managerCount[i] = items[i].GetComponent<ItemManager>().count;
data.managerCost[i] = (int)items[i].GetComponent<ItemManager>().cost;
}
bf.Serialize(file, data);
file.Close();
}
Answer by AnnoyingRain5 · May 26, 2016 at 10:13 PM
Put the floats at above public click click; but below public class game control I know this sounds silly but try it it might work
@AnnoyingRain5 Still the same error :( the problem started after adding those arrays.
Your answer
Follow this Question
Related Questions
Save/Load Animation State of Instantiated Prefabs 0 Answers
Saving and Loading Various Things 1 Answer
Serialized Data Not Saving on Android Build 0 Answers
Serialization Exception Error When Trying to Load 2 Answers
Serialization Location 1 Answer