Hi!
When I enter in Play Mode, it throws an “InvalidCastException: Specified cast is not valid.” at line highlighted in code below:
SaveableEntity.cs
public void RestoreState(object states)
{
Dictionary<string, object> stateDictionary = (Dictionary<string, object>)states; // here
foreach (var state in GetComponents<ISaveable>())
{
string typeString = state.GetType().ToString();
if (stateDictionary.ContainsKey(typeString))
{
state.RestoreState(stateDictionary[typeString]);
}
}
}
The method is called here:
SavingSystem.cs
private void RestoreState(Dictionary<string, object> stateDict)
{
foreach (var saveable in FindObjectsByType<SaveableEntity>(FindObjectsSortMode.None))
{
string id = saveable.GetUniqueIdentifier();
if (stateDict.ContainsKey(id))
{
saveable.RestoreState(stateDict[id]);
}
}
}
I cannot see why this error happens. Can someone enlighten me?