- Home /
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?
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.
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/
Adding the code to levelserializer.cs gives me a ton of errors, im unsure what I am doing wrong here.
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.
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.
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.
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.
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!
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?
Thanks Dude, this is Awesome. Just what I needed Appreciate it :)