hi…i have been trying to properly save an int value using serialization following unity’s data persistance tutorial but i am unable to do so.Basically i have a static int value(TotalStars) in my level selection scene and each time any of the 3 if’s statements that are in script of game scenes(levels) is true the value of TotalStars is incremented by 1.Well till here it works good,i can go to first level then if i win and come back to level selection i can see the value incremented But i want to save this value and load it when we again start the game which i am unable to do so.i dont know whether its unable to save the value or i am placing the save and load method at wrong places.I have tried various things but its not working .SO can anyone tell me am i doing it the write way and where i need to put the save and load methods so they automatically work? Here is the Script from level selection scene(i tried with and without singleton following the tutorial,though i dont think i needed it )
I don’t see where you are incrementing, I see a lot of data swaps but no +1 or ++ on anything …
I believe this is the error:
FileStream file = File.Create (Application.persistentDataPath + "/total.dat");
You should write it like this:
FileStream file = File.Open (Application.persistentDataPath + "/total.dat", FileMode.OpenOrCreate);
Because now you tell Unity to create a file over existing one. If this does not work, then I would suggest moving save and load to the class, have a class instance which would store the variable, and in save method, you would not need to create additional class. bf.Serialize (file, this);
would be enough.