- Home /
JSON Serialization and Instance IDs
I want to use ScriptableObjects as data containers that I will serialize with JSONUtility.
However references to GameObjects, MonoBehaviours or other ScriptableObjects are represented with an instance id in the resulting json file.
Can Unity properly hook up the correct instance if I deserialize the Object?
What about GameObjects that were created at runtime?
I'm struggling with the exact same issue.
using UnityEngine;
public class SerializerTest
{
public SerializerTest()
{
SimpleTest();
AdvancedTest();
}
void SimpleTest()
{
var foo = ScriptableObject.CreateInstance<Foo>();
foo.Number = 32;
Debug.Log(JsonUtility.ToJson(foo, true));
}
void AdvancedTest()
{
var firstFoo = ScriptableObject.CreateInstance<Foo>();
firstFoo.Number = 32;
var secondFoo = ScriptableObject.CreateInstance<Foo>();
firstFoo.Number = 16;
var bar = ScriptableObject.CreateInstance<Bar>();
bar.String = "testing";
bar.AFoo = firstFoo;
bar.BFoo = secondFoo;
Debug.Log(JsonUtility.ToJson(bar, true));
}
public class Foo : ScriptableObject
{
public int Number;
}
public class Bar : ScriptableObject
{
public string String;
public Foo AFoo;
public Foo BFoo;
}
}
Results in:
{
"Number": 32
}
and
{
"String": "testing",
"AFoo": {
"instanceID": -297306
},
"BFoo": {
"instanceID": -297308
}
}
I'm trying to sync stuff over network, and obviously those "instanceID"'s won't work!
I'm bumping the question because I'm facing the same issue.
Also have this problem. Im saving a list of ScriptableObjects, that I use for an EditorWindow, but get also their InstanceIDs in the JSON-File.
@Noxury - It might not be suitable in your case, but I've just used separate C# serializable classes to contain data I need. Before serialization, collect the ScriptableObject data to these data classes, then serialize these. When deserializing do the opposite. But I've worked very little with serialization yet.
Your answer

Follow this Question
Related Questions
Best Way to Store Large Number of GameObjects? 1 Answer
unity 3d loading data from json problem?! 1 Answer
JSON - Load Subset of Files Based on Key in File 0 Answers
MiniJSON Multiple Rows 1 Answer
JsonUtility not found / working 0 Answers