Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 CupOfThings · Aug 19, 2014 at 01:46 PM · serializationsaveloaddeleterestart

Unity Serializer - saves delete after restarting game

To save my game I am using the TestSerializer script which is attached to the Save Game Manager in my scene. My game saves correctly however after quitting and starting the game back up my saves all disappear apart from the very first game state that had been saved.

Ive heard that the problem could be that the Serializer is only allowed to save once, can someone help me with this?

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 Shugabush · Dec 28, 2021 at 11:10 PM 0
Share

Seriously what is the proper answer to this? I'm having this same problem, and the whydoidoit website link doesn't work anymore. I'm sick of troubleshooting this.

2 Replies

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

Answer by Cherno · Aug 20, 2014 at 03:11 PM

It could be that due to te array size, the saves are not persisting. This is a known bug with Unity Serializer. Here is a solution that works:

http://whydoidoit.com/forums/topic/saves-not-persisting-due-to-list-or-array-size/

Comment
Add comment · Show 27 · 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 CupOfThings · Aug 20, 2014 at 08:36 PM 0
Share

Adding the code to levelserializer.cs gives me a ton of errors, im unsure what I am doing wrong here.

avatar image Cherno · Aug 20, 2014 at 09:50 PM 0
Share

Well, since I can'tread your $$anonymous$$d and you don't explain exactly that kind of errors you get, I can say that if you do exactly as adamp says in this post:

http://whydoidoit.com/forums/topic/saves-not-persisting-due-to-list-or-array-size/#post-2613

it will work without a hitch.

avatar image CupOfThings · Aug 21, 2014 at 11:28 AM 0
Share

Unexpected symbol x - line 428

A local variable named stored' cannot be declared in this scope because it would give a different meaning - line 429 Unexpected symbol internal' - line 693

Sorry I'm not helping much here but I must be making a stupid error somewhere, Ive added the lines of code exactly how adamp says but it just wont work. This is what I have done.alt textalt text

428.jpg (163.5 kB)
688.jpg (188.4 kB)
avatar image Cherno · Aug 21, 2014 at 01:35 PM 0
Share

You made a couple of errors. The code from adamp is not exactly as in his post, look again:

in line 427

 SavedGames = 


and in the next line you declare a new variable. In adamp's code, ther is no "SavedGames = " line, just delete it, so the next line just starts with

 var x = File.OpenText(.....

Also, as you can see in his post, he has commented out the line

 //var stored = PlayerPrefs.GetString(“_Save_Game_Data_”);

So do that too, and you can declare the variable "stored" in line

 var stored = x.ReadToEnd (); // new code

without any problems.

About line 692: I can't see it on your image, but it it definitely looks like the save game directoy, which has to be provided as a string, is not. There seem to be what looks like quotation marks around the "/saves" bit, but it is not showing up yellow-orange as it should if it were a string. $$anonymous$$aybe you accidently used the a wrong symbol. Actually, if you look closely at his post, you will see that the quotation marks in his code were converted by the forum software so the first pair iss inverted. You probably just copied this, so just delete the marks in your code and type them in manually. Do this in every place where you copied quotation marks from the forum post.

avatar image CupOfThings · Aug 21, 2014 at 03:38 PM 0
Share

Okay I only have 1 error now saying "A local variable named `stored' cannot be declared in this scope" you said to declare the variable stored but how do I do this? thanks for the fast replies by the way.

Show more comments
avatar image
0

Answer by wesleywh · Aug 19, 2014 at 03:36 PM

This is my first post here. Call a load at the beginning. So on Awake() make sure that you have called your Load() function as well, if you created one. If you haven't you should make one. If you don't do that it will save everything fine but will not load them. Here is some of code to help you out:

 void Awake(){
         if (player == null) {//This creates one object that will be used to persist data between scenes
             DontDestroyOnLoad (this.gameObject);//Makes this a persistant object
             player = this;
             Load ();//This is where I am loading the values
         }
         else if (player != this) {
             Destroy (this.gameObject);
         }
     }
 
 public void Load(){//Grabs the file and pulls the data back out.
         if(File.Exists(Application.persistentDataPath + Path.PathSeparator +"Mazed.dat")){
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open (Application.persistentDataPath + Path.PathSeparator +"SomeFile.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             health = data.health;
             xp = data.xp; // this is loading the saved "XP" to the public xp int;
             gold = data.gold; // this is loading the saved "gold" to the public gold int;
             magic = data.magic; // this is loading the saved "magic" to the public magic int;
             spark_found = data.spell_spark;
 
             file.Close();
         }
     }
 public void Save(){//Saves data as a binary file to the proper folder for each OS.
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Create (Application.persistentDataPath + Path.PathSeparator +"SomeFile.dat");
         PlayerData data = new PlayerData ();
         data.health = health;
         data.xp = xp;
         data.magic = magic; //saves whatever is in "magic" to something called magic
         data.gold = gold; //saves whatever is in "gold" to something called gold
         data.spell_spark = spark_found; //saves whatever is in "spell_spark" to something called spell_spark
 
         bf.Serialize (file, data);
         file.Close ();
     }

I hope this helps!

Comment
Add comment · Show 2 · 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 CupOfThings · Aug 20, 2014 at 03:03 PM 0
Share

I dont fully understand what you mean, I am saving the player position and prefabs that the player is able to spawn into the game. Im guessing you mean to save each thing individually however this would be almost impossible to do as the player can spawn as many prefabs as he wants. Could you explain a bit more?

avatar image eric gemmell · Jan 01, 2016 at 05:48 PM 0
Share

Thanks Dude, this is Awesome. Just what I needed Appreciate it :)

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

7 People are following this question.

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

Related Questions

Character Positions Not Serializing or Loading Properly? 1 Answer

Unity Not Loading 0 Answers

How to save and load the game of the corresponding player by Serialization 1 Answer

Save/Load mutiple game objects as one file 1 Answer

Saving Game Problem 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