InvalidCastException: Cannot cast from source type to destination type.
Hey, I'm currently encountering an error and need to get this fixed ASAP for a large scale project.
Code below:
public void SaveFileExists(){
//GameObject.Find ("_Scripts").GetComponent<SaveGame> ().Load ();
//Creating a new BinaryFormatter
BinaryFormatter bf = new BinaryFormatter ();
//Making a file which now contains the Un-Serialized File
FileStream file = File.Open (Application.persistentDataPath + ("/UserData." + accountLoginAttempt), FileMode.Open);
//Assigning the values of the saved file to the new file
UserData newData = (UserData)bf.Deserialize (file);
file.Close ();
//Assigning the values from the save file to the variables in this script.
accountLoginAttempt = newData.username;
accountLevel = newData.userlevel;
//Assinging the values from this script to the _PlayerManager script. (This is where the UI gets it's info).
GameObject.Find ("_AccountInfoSaveHolder").GetComponent<HoldInfo> ().UserInfo = accountLoginAttempt;
GameObject.Find ("_AccountInfoSaveHolder").GetComponent<HoldInfo> ().UserLevel = accountLevel;
errortext.text = "Login Successful.";
Canvas.ForceUpdateCanvases ();
}
The serialised field is also below:
[System.Serializable]
class UserData{
public string username;
public int userlevel;
}
I am trying to load a savefile which is stored in the Application.persistentDataPath. Assigning the UserData.username and UserData.userlevel to variables in my script.
The error I am receiving is:
InvalidCastException: Cannot cast from source type to destination type.
Further Details:
LoginScript.SaveFileExists () (at Assets/Scripts/AccountSystem/LoginScript.cs:126) LoginScript.OnLoginClick () (at Assets/Scripts/AccountSystem/LoginScript.cs:104) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:634) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent.cs:769) UnityEngine.Events.UnityEvent.Invoke () (at /Users/builduser/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at /Users/builduser/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Any and all suggestions will be heard. I appreciate any help in advance.