When trying to load information, I am pulling a string (cubeName) from a Class called TheCubesName. The line “TheCubesName cubesName = (TheCubesName)bf.Deserialize(stream2);” is causing the error - Cannot cast from source type to destination type. And I am not sure why. When all connection to TheCubesName is commented out, everything else works properly. Since the Deserialize method woks for CubeDataV2, I do not understand why it fails to work with TheCubesName.
My Code
if(Directory.Exists(Application.persistentDataPath + "/SaveFiles/" + levelName))
{
Debug.Log("Can see the file");
BinaryFormatter bf = new BinaryFormatter();
FileStream stream2 = File.Open(Application.persistentDataPath + "/SaveFiles/" + levelName + "/savedObject.txt", FileMode.Open);
TheCubesName cubesName = (TheCubesName)bf.Deserialize(stream2);
CubeDataV2 cubesPosition = (CubeDataV2)bf.Deserialize(stream2);
GameObject spawnedObjects;
spawnedObjects = Instantiate(createObjs.cube, new Vector3(cubesPosition.transformX, cubesPosition.transformY, cubesPosition.transformZ), Quaternion.identity);
spawnedObjects.GetComponent<CubeName>().cubeName = cubesName.cubeName;
stream2.Close();
Any help would be appriciated. What am I missing here? Why could this error occuring?