Hi, I have a problem.
I cant work out why I cant seem to load and unload a class with three lists in it.
It keeps giving me this cast error:
InvalidCastException: Cannot cast from source type to destination type.
EndlessManagerScript.LoadChunk (Vector3 fileNamePos) (at Assets/GAME ASSETS/SCRIPTS/EndlessManagerScript.cs:298)
PlayerControlScript.Update () (at Assets/_CUSTOM ASSETS/SCRIPTS/PlayerControlScript.cs:204)
My class is:
[Serializable]
public class blockData
{
public List<string> objectNamesToSerialize = new List<string>();
public List<Int32> xPosToSerialize = new List<Int32>();
public List<Int32> yPosToSerialize = new List<Int32>();
}
My save method is:
try
{
Stream stream = File.Open(Application.dataPath + @"\Saves\Chunks\chunk " + thisChunkFilenameNumber, FileMode.CreateNew);
BinaryFormatter bformatter = new BinaryFormatter();
data.objectNamesToSerialize = objectNamesToSerialize;
data.xPosToSerialize = xPosToSerialize;
data.yPosToSerialize = yPosToSerialize;
bformatter.Serialize(stream, data);
stream.Close();
}
catch (UnauthorizedAccessException) { print("SAVING ERROR!!!"); } //many more exception might happen, check documentation
}
And my loading script is just:
public void LoadChunk(Vector3 fileNamePos)
{
{
Stream stream = new FileStream(Application.dataPath + @"\Saves\Chunks\chunk " + fileNamePos, FileMode.Open,FileAccess.Read);
BinaryFormatter bformatter = new BinaryFormatter();
data = new blockData();
data.objectNamesToSerialize = ((List<string>) bformatter.Deserialize(stream));
// here is where I've tried lots of different ways of reading it, i even mucked around with
// binaryReader and had the same problems. It cant seem to cast back into a new
// instance of the blockdata class for some reason.
stream.Close();
print(data.objectNamesToSerialize.Count);
File.Delete(Application.dataPath + @"\Saves\Chunks\chunk " + fileNamePos);
}
}
Not sure what I’m doing wrong. Thought I’d be able to save a list and then reload the list as is.
Could anyone help me please?