- Home /
Error on manually deserializing ScriptableObject
I'm working on a networked turn based game. I've created data structures that mirror the data stored on the server. I've set up serialization / deserialization so that I can encrypt the data and store it locally to the hard disk, so that is persists between play sessions (loads on start up, saves on application close). I've set up serialization / deserialization from JSON objects so that it can sync the data coming from the server.
All these classes extend from a common base class, which has some error handling built in to automatically report errors that may occur.
Now, I'm trying to convert the data classes to extend ScriptableObject. I don't really need Unity to serialize and save the data on its own, I just want it to be able to pass some of the data around in PlayMaker (which requires that it extend UnityEngine.Object). I also tried setting the hide flags in OnEnable to make it so that unity wouldn't save the data on its own, but I'm still getting the same error.
I modified the base class to extend ScriptableObject, cleared the local data file, ran it to generate fresh data, and closed it save the data.
Then when I run it again, and it tries to deserialize the saved data, it results in this error:
InvalidCastException: Value is not a convertible object: System.Boolean to Data.User System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Convert.cs:2941) System.Boolean.System.IConvertible.ToType (System.Type targetType, IFormatProvider provider) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Boolean.cs:235) System.Convert.ToType (System.Object value, System.Type conversionType, IFormatProvider provider, Boolean try_target_to_type) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Convert.cs:2937) System.Convert.ChangeType (System.Object value, System.Type conversionType) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Convert.cs:2490) System.Runtime.Serialization.FormatterConverter.Convert (System.Object value, System.Type type) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization/FormatterConverter.cs:47) System.Runtime.Serialization.SerializationInfo.GetValue (System.String name, System.Type type) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization/SerializationInfo.cs:149) Data.Root.DeserializeData (System.Runtime.Serialization.SerializationInfo info, StreamingContext context) (at Assets/Scripts/Data/Root.cs:34) Data.DeserializeExceptionCatcher..ctor (System.Runtime.Serialization.SerializationInfo info, StreamingContext context) (at Assets/Scripts/Data/DeserializationExceptionCatch.cs:28) Data.Root..ctor (System.Runtime.Serialization.SerializationInfo info, StreamingContext context) System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:513) Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation. System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:519) System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115) System.Runtime.Serialization.ObjectRecord.LoadData (System.Runtime.Serialization.ObjectManager manager, ISurrogateSelector selector, StreamingContext context) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs:578) System.Runtime.Serialization.ObjectManager.DoFixups () (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization/ObjectManager.cs:84) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (System.IO.BinaryReader reader) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:145) 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/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:110) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/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/monoAndRuntimeClassLibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136) DataStore.LoadEncrypted (System.String filePath, System.String encryptionKey, System.String encryptionIV) (at Assets/Scripts/Data/DataStore.cs:74) Data.DataStore.LoadDataFromDisk () (at Assets/Scripts/Data/DataStore.cs:69) PlayMaker.LoadDataFromDisk.OnEnter () (at Assets/Scripts/PlayMaker/Actions/LoadDataFromDisk.cs:25) HutongGames.PlayMaker.FsmState.ActivateActions (Int32 startIndex) HutongGames.PlayMaker.FsmState.OnEnter () HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) HutongGames.PlayMaker.Fsm.UpdateStateChanges () HutongGames.PlayMaker.Fsm.Start () PlayMakerFSM.Start ()
Here are my scripts:
BaseData.cs
using System;
using System.Runtime.Serialization;
using UnityEngine;
using Boomlagoon.JSON;
namespace Data
{
[Serializable]
public class BaseData : ScriptableObject
{
#region Constructors
public BaseData() {}
// Constructor used by ISerializable to create object by deserializing data
public BaseData(SerializationInfo info, StreamingContext context)
{
try
{
DeserializeData(info, context);
}
catch (SerializationException e)
{
ExceptionManager.instance.ReportException(e);
}
}
public BaseData(JSONObject data)
{
try
{
// do deserialization
SyncFromJSON(data);
}
catch (SerializationException e)
{
ExceptionManager.instance.ReportException(e);
Debug.LogWarning("Reporting the following exception: " + e.Message);
}
}
protected virtual void DeserializeData(SerializationInfo info, StreamingContext context) {}
public virtual void SyncFromJSON(JSONObject data) {}
#endregion Constructors
}
}
and User.cs
using System;
using System.Runtime.Serialization;
using UnityEngine;
using Boomlagoon.JSON;
namespace Data
{
[Serializable]
public class User : BaseData
{
#region Properties
public string id;
public string authToken;
public long last_sync_date;
public bool accept_random_game_invites;
public Games games;
#endregion Properties
#region Constructors
public User() {}
public User(SerializationInfo info, StreamingContext context) : base(info, context) {}
public User(JSONObject data) : base (data) {}
#endregion Constructors
#region ISerializable Implementation
// Deserialiation Method
protected override void DeserializeData(SerializationInfo info, StreamingContext context)
{
id = (string) info.GetValue("id", typeof(string));
authToken = (string) info.GetValue("authToken", typeof(string));
last_sync_date = (long) info.GetValue("last_sync_date", typeof(long));
accept_random_game_invites = (bool) info.GetValue("accept_random_game_invites", typeof(bool));
games = (Games) info.GetValue("games", typeof(Games));
}
// Serialization method
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("id", id);
info.AddValue("authToken", authToken);
info.AddValue("last_sync_date", last_sync_date);
info.AddValue("accept_random_game_invites", accept_random_game_invites);
info.AddValue("games", games);
}
#endregion ISerializable Implementation
#region JSON Sync Implementation
public override void SyncFromJSON(JSONObject data)
{
Debug.LogWarning("User::SyncFromJSON");
Debug.Log(data.ToString());
// Sync User Properties
// TODO: double check username and token
if (data.ContainsKey("last_sync_date"))
last_sync_date = (long) data.GetNumber("last_sync_date");
accept_random_game_invites = data.GetBoolean("accept_random_game_invites");
// Sync Game Data
if (games == null)
games = new Games();
games.SyncFromJSON(data.GetObject("games"));
}
#endregion JSON Sync Implementation
#region Network Sync Interface
public void ProcessNewlyCreatedPlayerJSON(JSONObject data)
{
id = data.GetString("user_id");
authToken = data.GetString("token");
Debug.Log("Processing Data on new user (" + id + ")");
}
#endregion Network Sync Interface
}
}
Any ideas on what is breaking this? It worked fine before I started extending ScriptableObject (and works fine if I remove it and reset the data)
Your answer

Follow this Question
Related Questions
ScriptableObject loses data after Reload 1 Answer
How to serialize scripts in Unity? 1 Answer
List of ScriptableObjects lost on project reload 1 Answer
ScriptableObject serialize object,any work around 1 Answer
Wrong serialization when using ScriptableObject + [System.Serializable] + inheritance 2 Answers