- Home /
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.
Answer by Bunny83 · Jun 09, 2018 at 10:09 AM
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.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
type not found 1 Answer
Sync with visual studio 2010 error 1 Answer
No Monobehaviour scripts in files 1 Answer