• 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 dreadthread · Sep 04, 2016 at 03:37 PM · loadingsavingloading file

SerializationException: serializationStream supports seeking, but its length is 0 when trying to load a save game

For whatever reason, my saving and loading code does not work as the save file always turns out to be blank for a reason that I cannon decipher. Here is my code:

 public class SaveManager : MonoBehaviour {

 public static SaveManager saveManager;

 private HealthController healthController;
 // Use this for initialization
 void Awake () {
     healthController = GameObject.FindObjectOfType<HealthController>();
     if(saveManager == null)
     {
         DontDestroyOnLoad(gameObject);
         saveManager = this;
     }else if (saveManager != this)
     {
         Destroy(gameObject);
     }
     
 }

 public void Save()
 {
     BinaryFormatter bf = new BinaryFormatter();
     FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
     PlayerData data = new PlayerData();

     data.damage = Player.damage;
     data.shotspeed = Player.speedOfLaser;
     data.speed = Player.speed;
     data.fireRate = Player.firingRate;
     data.currentHealth = healthController.currentHealth;
     data.maxHealth = HealthController.maxHealth;

     data.skillPoints = Player.skillPoints;
     data.currentDamageUpgrades = UpgradeManager.currentDamageUpgrades;
     data.currentMaxHealthUpgrades = UpgradeManager.currentMaxHealthUpgrades;
     data.currentSpeedUpgrades = UpgradeManager.currentSpeedUpgrades;
     data.currentShotSpeedUpgrades = UpgradeManager.currentShotSpeedUpgrades;
     data.currentFiringRateUpgrades = UpgradeManager.currentFiringRateUpgrades;

     data.currentWave = WaveSpawner.currentWave;
     data.nextWave = WaveSpawner.nextWave;
     bf.Serialize(file, data);
     file.Close();
 }

 public void Load()
 {
     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();

         Player.damage = data.damage;
         Player.speedOfLaser = data.shotspeed;
         Player.speed = data.speed;
         Player.firingRate = data.fireRate;
         healthController.currentHealth = data.currentHealth;
         HealthController.maxHealth = data.maxHealth;

         Player.skillPoints = data.skillPoints;
         UpgradeManager.currentDamageUpgrades = data.currentDamageUpgrades;
         UpgradeManager.currentMaxHealthUpgrades = data.currentMaxHealthUpgrades;
         UpgradeManager.currentSpeedUpgrades = data.currentSpeedUpgrades;
         UpgradeManager.currentShotSpeedUpgrades = data.currentShotSpeedUpgrades;
         UpgradeManager.currentFiringRateUpgrades = data.currentFiringRateUpgrades;

         WaveSpawner.currentWave = data.currentWave;
         WaveSpawner.nextWave = data.nextWave;
     }
 }

 }

    [Serializable]
   class PlayerData
 {
 public float shotspeed, speed, fireRate;
 public int damage, currentHealth, maxHealth, skillPoints, currentDamageUpgrades, currentMaxHealthUpgrades, currentSpeedUpgrades, currentShotSpeedUpgrades, currentFiringRateUpgrades;
 public int currentWave, nextWave;
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dreadthread · Sep 04, 2016 at 03:23 PM

I figured out the answer. The problem was with the DontDestroyOnLoad. I removed that and everything worked!

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Load file on Android 1 Answer

Newbie questions! (Variables and Saving) 2 Answers

Saving Levels 2 Answers

How do you save a player's prefered resolution in Unity 2017.1.1f? 0 Answers

Saving and loading to/from Android 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