public void SaveFileExists() Debug.Log ("SaveFileExists was called."); //GameObject.Find ("_Scripts").GetComponent ().Load (); UserData userdata = null; //Creating a new BinaryFormatter BinaryFormatter bf = brand-new BinaryFormatter (); //Making a document which now has the Un-Serialized file FileStream document = File.Open (Application.persistentDataPath + ("/UserData." + accountLoginAttempt), FileMode.Open); //Assigning the worths of the saved document to the new record //UserData newData = (UserData)bf.Deserialize (file); userdata = bf.Deserialize(file) as UserData; //Assigning the values from the save file to the variables in this script. AccountLoginAttempt = userdata.username; accountLevel = userdata.userlevel; //Assinging the values from this manuscript to the _PlayerManager script. (This is where the UI it s okay it"s info).


You are watching: Invalidcastexception: cannot cast from source type to destination type.


See more: Hotel Econo Lodge Arena Wilkes-Barre Pa, Econo Lodge Arena Wilkes Barre

GameObject.Find ("_AccountInfoSaveHolder").GetComponent ().UserInfo = accountLoginAttempt; GameObject.Find ("_AccountInfoSaveHolder").GetComponent ().UserLevel = accountLevel; errortext.text = "Login Successful."; Canvas.ForceUpdateCanvases ();The serialised ar is likewise below:

class UserDatapublic cable username;public int userlevel;I am trying to fill a savefile which is save on computer in the Application.persistentDataPath. Assigning the UserData.username and UserData.userlevel come variables in mine script.

The error ns am receiving is:

InvalidCastException: Cannot cast from source form to location 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 (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()

EDIT:

SaveGame.cs:

using System.Collections;using System.Collections.Generic;using UnityEngine;using System.Runtime.Serialization.Formatters.Binary; using System.IO;using UnityEngine.SceneManagement;public course SaveGame : MonoBehaviour //Saved Variablespublic wire savedname;public string attemptedlogin;public int savedlevel;//Loaded Variablespublic cable loadedname;public int loadedlevel;public void Awake() if (SceneManager.GetActiveScene ().name == "GameScene") load (); public void Save() savedname = GameObject.Find ("_Scripts").GetComponent ().PlayerName; savedlevel = GameObject.Find ("_Scripts").GetComponent ().UserLevel; BinaryFormatter bf = brand-new BinaryFormatter(); FileStream record = File.Open(Application.persistentDataPath + ("/UserData." + savedname), FileMode.Create); UserData newData = brand-new UserData(); newData.username = savedname; newData.userlevel = savedlevel; bf.Serialize(file, newData); file.Close();I also removed every System.Serializable class"s transparent my scripts and made a script referred to as UserDataSerialize.cs which contains:

using System.Collections;using System.Collections.Generic;using UnityEngine;class UserData windy string username; windy int userlevel;Now the error I acquire is:

TypeLoadException: could not load kind "SaveGame+UserData".System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError, Boolean ignoreCase) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/Assembly.cs:398)System.Reflection.Assembly.GetType (System.String name, Boolean throwOnError) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/Assembly.cs:381)System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetDeserializationType (Int64 assemblyId, System.String className) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:833)System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadTypeMetadata (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:637)System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectInstance (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:269)System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:195)System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:223)System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:130)System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header<>& headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:104)System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:179)System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136)LoginScript.SaveFileExists () (at Assets/Scripts/AccountSystem/LoginScript.cs:111)LoginScript.OnLoginClick () (at Assets/Scripts/AccountSystem/LoginScript.cs:84)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 (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()