Serialisation: InvalidCastException: Cannot cast from source type to destination type.

Trying to save and load a custom class (SavedChordProgression) on the game starting up. After reading up on the error, I understand why I can’t cast down the inherited hierarchy. However, I am no closer to working out the solution to my problem.

    private SavedChordProgression listOfSaves = new SavedChordProgression();
    public void save()
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(Application.persistentDataPath + "/savedGames.mi");
        bf.Serialize(file, listOfSaves);
        file.Close();
    }

    public void load()
    {
        if (File.Exists(Application.persistentDataPath + "/savedGames.mi"))
        {
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(Application.persistentDataPath + "/savedGames.mi", FileMode.Open);
            listOfSaves = (SavedChordProgression)bf.Deserialize(file);
            file.Close();

            loadAllSaveObjects();
        }
    }

Thanks very much for your help.

This code should work just find as long as your “SavedChordProgression” is marked as serializable. Are you sure you don’t have an older save in that file? try deleting the old save and create a new one. The binary formatter is extremely strict. If this doesn’t work you should include your “SavedChordProgression” in your question.