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?