Try to change your
GameData
class and use public properties instead.
[Serializable]
class GameData
{
public bool IsGameStartedFirstTime { get; set; }
public bool IsMusicOn { get; set; }
public int HighSchore { get; set; }
public bool[] Levels { get; set; }
}
[UPDATE]
Well, I thought you had a runtime error not a compiler error.
This line
bf.Serialize(data, file);
should be
bf.Serialize(file, data);
(Maybe open the help???)
Try this code as a working sample:
public void Save()
{
try
{
GameData data = new GameData();
data.HighSchore = 1024;
data.IsGameStartedFirstTime = true;
data.IsMusicOn = false;
data.Levels = new bool[10];
data.Levels[0] = true;
data.Levels[1] = true;
using (FileStream file = File.Create(@"C:\Temp\Gamedata.data"))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, data);
}
GameData dataFromFile = new GameData();
using (FileStream file = File.OpenRead(@"C:\Temp\Gamedata.data"))
{
BinaryFormatter bf = new BinaryFormatter();
dataFromFile = (GameData)bf.Deserialize(file);
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}