0
votes

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

1
You might consider deleting this question - Jeff

1 Answers

0
votes

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);
    }
}