What happens when I cast an object to a class and the object does not have a variable that I have in my class? In the below example, I am loading from a FileStream which I serialized from a PlayerData before. However at the time of the serializing there was no score variable included in PlayerData, so the file does not contain that. If I load like below, what happens to the score variable? Is there a way to set the value to zero whenever a situation like this arises ?
public string userID;
public bool userCreated;
public double score;
public void Load () {
if (File.Exists (Application.persistentDataPath + "/SaveGame.dat")) {
Debug.Log ("Loading from local space");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/SaveGame.dat", FileMode.Open);
PlayerData data = (PlayerData) bf.Deserialize (file); // Question pertains this step
file.Close ();
userID = data.userID;
userCreated = data.userCreated;
score = data.score;
}
}
[System.Serializable]
class PlayerData {
public string userID;
public bool userCreated;
public double score;
}