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 /
  • Help Room /
avatar image
0
Question by NinjaboyGames · Dec 06, 2017 at 12:28 AM · scripting problemserializationloadingsavingclasses

Can't figure out how to serialize my save data...

Hello, I'm currently trying to figure out why my save data isn't saving correctly. It's currently writing a blank file with nothing inside of it, and I can't figure out why. Here's the code:

   public void Save(int slot) //4 slots
     {
         PlayerPrefs.SetInt("SaveSlot",saveSlot);
 
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/save1.VortrusSave");
 
         SaveData data = new SaveData();
 
         data.VoidSectorsUnlocked[slot] = VoidSectorsUnlocked;
         data.VenetianClusterUnlocked[slot] = VenetianClusterUnlocked;
         data.VoidSectorBestScore[slot] = voidSectors_Best;
         data.VenetianClusterBestScore[slot] = venetianCluster_Best;
         data.AfterburnerUnlocked[slot] = AfterburnerUnlocked;
         data.StarrunnerUnlocked[slot] = StarrunnerUnlocked;
 
         bf.Serialize(file,data);
         file.Close();
 
     }
 
     public void Load(int slot)
     {
 
         if (File.Exists(Application.persistentDataPath + "/save1.VortrusSave"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/save1.VortrusSave", FileMode.Open);
 
             SaveData data = (SaveData)bf.Deserialize(file);
             Debug.Log("It exists...");
 
             file.Close();
 
             VoidSectorsUnlocked = data.VoidSectorsUnlocked[slot];
             VenetianClusterUnlocked = data.VenetianClusterUnlocked[slot];
             voidSectors_Best = data.VoidSectorBestScore[slot];
             venetianCluster_Best = data.VenetianClusterBestScore[slot];
             AfterburnerUnlocked = data.AfterburnerUnlocked[slot];
             StarrunnerUnlocked = data.StarrunnerUnlocked[slot];
 
         }
     }
 
 [Serializable]
 class SaveData
 {
 
     //Save names 
     public string[] SaveNames;
 
     //Unlocked Levels
     public int[] VoidSectorsUnlocked;
     public int[] VenetianClusterUnlocked;
 
     //Unlocked spaceships
     public int[] StarrunnerUnlocked;
 
     //Unlocked spaceship parts
     public int[] AfterburnerUnlocked;
 
     //Saved scores
     public int[] VoidSectorBestScore;
     public int[] VenetianClusterBestScore;
 
 }
Comment
Add comment · Show 1
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 agray427 · Jan 16, 2018 at 08:02 PM 0
Share

Is it because SaveData isn't public? It all looks fine to me!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NinjaboyGames · Mar 06, 2018 at 06:45 AM

Figured it out for anyone reading - the serialization does not allow for expandable data types so those arrays are not allowed. For example, int is allowed, but int[] is not.

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.

Follow this Question

Answers Answers and Comments

176 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Setting components variables with another component 0 Answers

Instantiating objects only after a scene is loaded 1 Answer

Loading and Saving error 1 Answer

Variables changes even on pause 0 Answers

Serializing name of the current level 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