Specified cast is not valid.

Hi, I’m working on a plane flight simulator. But the save data and load data isn’t working correctly.

I keep getting the error

InvalidCastException: Specified cast is not valid.
PlayerControllerX4.LoadFile () (at Assets/Challenge 1/Scripts/PlayerControllerX4.cs:54)
PlayerControllerX4.Start () (at Assets/Challenge 1/Scripts/PlayerControllerX4.cs:73)```

My script that creates the save data looks like so:

```csharp
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class PlayerControllerX3 : MonoBehaviour
{

    //Declaring Variables
    public float speedAI;
    public float rotationSpeedAI;
    public float verticalInputAI;
    public float horizontalInputAI;
    public List<float> inputsHorizontal = new List<float>();
    public List<float> inputsVertical = new List<float>();
    public int i;
    public int timer;
    //Class and class constructor for data to save
    [System.Serializable]
    public class PlaneData
    {
        public List<float> inputsHorizontalRec = new List<float>();
        public List<float> inputsVerticalRec = new List<float>();

        public PlaneData(List<float> inputH, List<float> inputV)
        {
            inputsHorizontalRec = inputH;
            inputsVerticalRec = inputV;
        }
    }

    //Save file operation
    public void SaveFile()
    {
        string destination = "save.dat";
        FileStream file;

        if (File.Exists(destination)) file = File.OpenWrite(destination);
        else file = File.Create(destination);

        PlaneData data = new PlaneData(inputsHorizontal, inputsVertical);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(file, data);
        file.Close();
    }

   


    void FixedUpdate()
    {
        //Random Inputs
        verticalInputAI = (Random.Range(-0.3f, 0.3f));
        horizontalInputAI = (Random.Range(-0.8f, 0.8f));

        // move the plane forward at a constant rate
        transform.Translate(Vector3.forward * speedAI * Time.deltaTime);
        //Plane rotations
        transform.Rotate(Vector3.right * rotationSpeedAI/2 * Time.deltaTime * verticalInputAI);
        transform.Rotate(Vector3.up * rotationSpeedAI/4 * Time.deltaTime * horizontalInputAI);
        inputsHorizontal.Add(horizontalInputAI);
        inputsVertical.Add(verticalInputAI);

        //Call save operation

        if (i > timer)
        {
            SaveFile();
        }
        else { i++; }
    }
}

My load script is this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

public class PlayerControllerX4 : MonoBehaviour
{

    //Class and class constructor for data to save
    [System.Serializable]
    public class PlaneData
    {
        public List<float> inputsHorizontalRec = new List<float>();
        public List<float> inputsVerticalRec = new List<float>();

        public PlaneData(List<float> inputH, List<float> inputV)
        {
            inputsHorizontalRec = inputH;
            inputsVerticalRec = inputV;
        }
    }

   

    //Save file operation
    public void SaveFile()
    {
        string destination = "save.dat";
        FileStream file;

        if (File.Exists(destination)) file = File.OpenWrite(destination);
        else file = File.Create(destination);

        PlaneData data = new PlaneData(inputsHorizontal, inputsVertical);
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(file, data);
        file.Close();
    }
    //Load Function
    public void LoadFile()
    {
        string destination = "save.dat";
        FileStream file;

        if (File.Exists(destination)) file = File.OpenRead(destination);
        else
        {
            Debug.LogError("File not found");
            return;
        }

        BinaryFormatter bf = new BinaryFormatter();
        PlaneData data = (PlaneData) bf.Deserialize(file);
        file.Close();


    }

    //Declaring Variables
    public float speedAI;
    public float rotationSpeedAI;
    public float verticalInputAI;
    public float horizontalInputAI;
    public List<float> inputsHorizontal = new List<float>();
    public List<float> inputsVertical = new List<float>();
    public int i;
    public int timer;
    public int isecond;
    public List<float> inputsHorizontalAuto = new List<float>();
    public List<float> inputsVerticalAuto = new List<float>();

    void Start() { LoadFile(); }


    void FixedUpdate()
    {
        /*
        //Random Inputs
        verticalInputAI = (Random.Range(-0.3f, 0.3f));
        horizontalInputAI = (Random.Range(-0.8f, 0.8f));

        // move the plane forward at a constant rate
        transform.Translate(Vector3.forward * speedAI * Time.deltaTime);
        //Plane rotations
        transform.Rotate(Vector3.right * rotationSpeedAI/2 * Time.deltaTime * verticalInputAI);
        transform.Rotate(Vector3.up * rotationSpeedAI/4 * Time.deltaTime * horizontalInputAI);
        inputsHorizontal.Add(horizontalInputAI);
        inputsVertical.Add(verticalInputAI);

        //Call save operation
        if (i > timer)
        {
            SaveFile();
        }
        else { i++; }
        */
        if (Input.GetKey(KeyCode.L))
        {
            isecond++;
            transform.Rotate(Vector3.right * rotationSpeedAI / 2 * Time.deltaTime * inputsHorizontalAuto[isecond]);
            transform.Rotate(Vector3.up * rotationSpeedAI / 4 * Time.deltaTime * inputsVerticalAuto[isecond]);
        }
    }
}

I am so confused on why this keeps happening, to me the cast types seem correct and everything matches. Where am I going wrong?

They seem incorrect to binary formatter.

Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide

Load/Save steps:

https://discussions.unity.com/t/799896/4

An excellent discussion of loading/saving in Unity3D by Xarbrough:

https://discussions.unity.com/t/870022/6

Loading/Saving ScriptableObjects by a proxy identifier such as name:

https://discussions.unity.com/t/892140/8

When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls new to make one, it cannot make the native engine portion of the object.

Instead you must first create the MonoBehaviour using AddComponent() on a GameObject instance, or use ScriptableObject.CreateInstance() to make your SO, then use the appropriate JSON “populate object” call to fill in its public fields.

If you want to use PlayerPrefs to save your game, it’s always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

1 Like