I'm trying to serialize an object like this:
public void SaveHighscore()
{
string fname = Application.loadedLevelName + "_score.dat";
using (Stream stream = File.Open(fname, FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, this);
}
}
Highscore only has data type members and one object of type Replay, with again only has data type members as well as two lists of serializable objects.
I'm getting
SerializationException: Type GameController is not marked as Serializable.
Gamecontroller is the class that calls object.SaveHighscore(). It's not referenced anywhere within the highscore object itself.
edit: solved. I had an event in the class which was registered by gameController
With the code shown, you are not trying to serialize your highscore anywhere.
By using bin.Serialize(stream, this);
, you are serializing the calling class into the stream (because of the this keyword ). Since the current class is GameController
, it's trying to serialize it.
You should pass an instance of the highscore you want to save into the method and serialize that instead :
public void SaveHighscore(HighScore highscore)
{
string fname = Application.loadedLevelName + "_score.dat";
using (Stream stream = File.Open(fname, FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, highscore);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.