- Home /
Filestream/SaveManager
Hello!
I created a save/load script by following a guide on youtube, but I get this error when SaveLoad.Load(); is called:
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream, System.Object graph) [0x00000] in :0 at SaveLoad.Save () [0x00000] in :0 (Filename: Line: -1)
public static void Save ()
{
BinaryFormatter binary = new BinaryFormatter ();
FileStream fStream = File.Create (Application.persistentDataPath + "/turbocoinSaveFile.sav");
SaveManager sManager = new SaveManager ();
sManager.coins = CoinController.coins;
sManager.coinsTotalEver = CoinController.coinsTotalEver;
sManager.totalTurbosEver = Statistics.totalTurbosUsed;
binary.Serialize (fStream, sManager);
fStream.Close ();
}
public static void Load ()
{
if (File.Exists (Application.persistentDataPath + "/turbocoinSaveFile.sav")) {
BinaryFormatter binary = new BinaryFormatter ();
FileStream fStream = File.Open (Application.persistentDataPath + "/turbocoinSaveFile.sav", FileMode.Open);
SaveManager sManager = (SaveManager)binary.Deserialize (fStream);
fStream.Close ();
CoinController.coins = sManager.coins;
CoinController.coinsTotalEver = sManager.coinsTotalEver;
Statistics.totalTurbosUsed = sManager.totalTurbosEver;
}
}
[Serializable]
class SaveManager
{
public int coins;
public int coinsTotalEver;
public int totalTurbosEver;
}
The error you copied seems to reference the SaveLoad.Save () method. Also can you include your definition for Save$$anonymous$$anager? Are they all simple types in it?
[Serializable]
class Save$$anonymous$$anager
{
public int coinsTotalEver;
public int totalTurbosEver;
}
Oh yes there is, I accidentally deleted it from my comment. :)
[Serializable]
class Save$$anonymous$$anager
{
public int coins;
public int coinsTotalEver;
public int totalTurbosEver;
}
Answer by kmgr · Jun 26, 2015 at 09:22 AM
Might be an issue with IDisposable interface, as you're not calling Dispose() explicitly. Try wraping I/O stuff in the 'using' clause.
void Save()
{
using(FileStream fs = File.Create("filename"))
{
BinaryFormatter binf = new BinaryFormatter();
binf.Serialize(fs, saveData);
}
}
How would this look with 'using'?
public static void Save ()
{
BinaryFormatter binary = new BinaryFormatter ();
FileStream fStream = File.Create (Application.persistentDataPath + "/turbocoinSaveFile.sav");
Save$$anonymous$$anager s$$anonymous$$anager = new Save$$anonymous$$anager ();
s$$anonymous$$anager.coins = CoinController.coins;
s$$anonymous$$anager.coinsTotalEver = CoinController.coinsTotalEver;
s$$anonymous$$anager.totalTurbosEver = Statistics.totalTurbosUsed;
binary.Serialize (fStream, s$$anonymous$$anager);
fStream.Close ();
}
void Save()
{
using(FileStream fs = File.Create(Application.persistentDataPath + "/turbocoinSaveFile.sav"))
{
BinaryFormatter binf = new BinaryFormatter();
Save$$anonymous$$anager s$$anonymous$$anager = new Save$$anonymous$$anager ();
s$$anonymous$$anager.coins = CoinController.coins;
s$$anonymous$$anager.coinsTotalEver = CoinController.coinsTotalEver;
s$$anonymous$$anager.totalTurbosEver = Statistics.totalTurbosUsed;
binf.Serialize(fs, s$$anonymous$$anager);
}
}
Load() method would only be different in the way that you call Deserialize, but the 'using' stuff looks the same.
I would move File.Exists outside, so that you're first checking if the file exists and the open it. Also in the Load() method you should call File.OpenRead() not File.Create()
void Load()
{
if (File.Exists("Application.persistentDataPath + "/turbocoinSaveFile.sav"))
{
using(FileStream fs = File.OpenRead("Application.persistentDataPath + "/turbocoinSaveFile.sav"))
{
BinaryFormatter binf = new BinaryFormatter();
Save$$anonymous$$anager s$$anonymous$$anager = (Save$$anonymous$$anager)binf.Deserialize(fs);
}
}
}
Answer by Clint.Carpenter · Jun 26, 2015 at 05:19 PM
Looking again it seems like the error is in your save function specifically with your file. Check that fileStream != null after you create it.
Are you running this code from the editor? you are not by chance trying to run this from a web application are you? Because then the persistentDataPath would not work.
There goes that theory.
Last thing I can think to try is to hard code the values you are putting into Save$$anonymous$$anager just to make sure it isn't a error with the data (like null or uninitialized)
what if I added if (!= null) to each value? Would it work?
For example:
if (CoinController.coins != null) {
s$$anonymous$$anager.coins = CoinController.coins;
}
I am shooting in the dark here, but I would try:
s$$anonymous$$anager.coins = 2; s$$anonymous$$anager.coinsTotalEver = 3; s$$anonymous$$anager.totalTurbosEver = 4;
Then at least you can be certain the data is or IS NOT part of the problem.
Not working with:
public static void Save ()
{
BinaryFormatter binary = new BinaryFormatter ();
FileStream fStream = File.Create (Application.persistentDataPath + "/playerData.sav");
Save$$anonymous$$anager s$$anonymous$$anager = new Save$$anonymous$$anager ();
if (PlayerPrefs.Has$$anonymous$$ey ("Coins")) {
s$$anonymous$$anager.coins = CoinController.coins;
}
if (PlayerPrefs.Has$$anonymous$$ey ("TotalCoinsEver")) {
s$$anonymous$$anager.coinsTotalEver = CoinController.coinsTotalEver;
}
if (PlayerPrefs.Has$$anonymous$$ey ("TotalTurbosUsed")) {
s$$anonymous$$anager.totalTurbosEver = Statistics.totalTurbosUsed;
}
binary.Serialize (fStream, s$$anonymous$$anager);
fStream.Close ();
}
Your answer
Follow this Question
Related Questions
Coding help: How to use xml serialization 1 Answer
The name "Game Foundation" does not exist in the current context 1 Answer
Multiple Cars not working 1 Answer
How do I save and load the state of the GameObjects with PlayerPrefs? 0 Answers
[Solved] Built in GUI continuously Loads through different Options. 1 Answer