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 kdsarora · May 26, 2016 at 01:06 PM · c#serializationloadingsavingbinaryformatter

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;
     }


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 ninja_gear · May 27, 2016 at 03:13 AM 0
Share

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...

  1. what line does the error occur on?

  2. what does the 'Click' class look like?

  3. 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

avatar image kdsarora ninja_gear · May 27, 2016 at 04:52 AM 0
Share

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.

avatar image ninja_gear kdsarora · May 27, 2016 at 06:06 AM 0
Share

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.

Show more comments

2 Replies

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

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();
     }

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 kdsarora · May 27, 2016 at 06:31 AM 0
Share

It works!!!!! Thanks a Ton!! :) :)

avatar image
0

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

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 kdsarora · May 27, 2016 at 02:31 AM 0
Share

@AnnoyingRain5 Still the same error :( the problem started after adding those arrays.

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

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


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