Specified Cast Error with Bonus section of saving

I liked the saving section of this course - really insightful and clever. I especially like the dictionary option but get an intriguing error:

InvalidCastException: Specified cast is not valid…

this is the offending line of code: SerializableVector3 position = (SerializableVector3)state;
from the RestoreState(object state)

What is intriguing is the compiler knows its needs a cast, but it sure doesn’t like it. Thoughts or did I miss something in Sam’s lecture?

Post up your Capture and Restore State and the structure that you are trying to save and I’ll take a look.

        public object CaptureState()
        {
            Dictionary<string, object> data = new Dictionary<string, object>();
            data["position"] = new SerializableVector3(transform.position);
            data["rotation"] = new SerializableVector3(transform.eulerAngles);
            return data;
        }

        public void RestoreState(object state)
        {
            Dictionary<string, object> data = (Dictionary<string, object>)state;
            SerializableVector3 position = (SerializableVector3)state;
            GetComponent<NavMeshAgent>().enabled = false;
            transform.position = ((SerializableVector3)data["position"]).ToVector();
            transform.eulerAngles = ((SerializableVector3)data["rotation"]).ToVector();
            GetComponent<NavMeshAgent>().enabled = true;
        }

I believe this is the offending line. It’s unneeded, as you’re not using the Dictionary.

Brian,

I know the remaining class does NOT use dictionaries but Sam created this library and said we could which is why he gave us this code exactly. However, I am getting the weird cast error when trying to run Sam’s code as shown above. I was curious if there is a mistake somewhere or Sam has another solution we should use instead. I WANT to use Dictionaries.

You can use Dictionaries, and the first cast

Dictionary<string, object> data = (Dictionary<string,object>)state;

should work fine, since that’s what was returned to the Saving System (in fact it did work fine, or you wouldn’t have to gotten to the invalid cast on the next line)
The second line

SerializeableVector3 position = (SerializableVector3)state;

is leftover from the original Mover code. It can’t convert the state (which is really a Dictionary) to a SerializableVector3. You’ll note that position is never used in the rest of the routine. You should be able to remove that line of code, and it will work fine.

1 Like

Brian,

I just came to that conclusion - and was coming here to leave a note. Your explanation is spot on. I am not sure how I missed it. I swear Sam leaves that line in during the video.

1 Like

I had to double check (because if he didn’t, I needed to make a patch request). He actually selects the original cast to ScriptableObject and overwrites it with the new cast to the Dictionary

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms