Hey guys, i am making a game with choices, like an RPG talk system
but i can make more than 7 lines because the unity serialize only seven
error:Serialization depth limit exceeded
using UnityEngine;
using System.Collections;
[System.Serializable]
public class class_a{
public string talk;
public class_a[] _a;
}
public class DialogList : MonoBehaviour {
public class_a depth;
}
note: cant be with nonserializable because i have to edit in the inspector
note 2 : Unity 5
unity’s serialization system for non-unity types is not referenced based, but value base.
So if you have a class reference an object of the same type (or an object, that references an object of the same type), the class_a in it will be serialized by value.
The result is recursive serialization… infinite loop.
class_a serializes a class_a which serializes a class_a which serializes a class_a which serializes a class_a… ad infinum
If you’re trying to make a link-list like this, you should not serialize the node links, and instead break the entire list out into an array for serialization.
Unity’s serializer is not exceptionally bright when it comes to nested classes. It basically doesn’t understand the concept of ‘null’, nor really of ‘objects’. Your actual data might look like this:
depth -> _a -> null
But Unity’s serializer doesn’t see it that way. It will see:
depth -> _a -> _a=null -> _a=null -> _a=null.....
Because it just doesn’t stop when _a is null, it expands everything that that class could have in it. And because your class is recursive, that it could be infinite. This “7 layers” message is Unity’s own cutoff, to prevent the serializer from getting into an infinite loop.
The only solution I’m aware of is to simply not use Unity’s serializer in situations like this. You could, for example, serialize with JSON instead, which I believe will not have this problem.
Our company uses SimpleJSON (well, a modification of it), but we’ve been using that for a long time, and since then Unity 5.3 introduced native JSON support, though I haven’t experimented with it.
You’ll have to perform the serialization yourself… most common technique to do it directly in a script is to use ISerializationCallbackReceiver:
The 2 methods are called before and after serialization. You would mark the field/var of your script as [System.NonSerialized()]. And then a private field marked as [SerializedField()] that stores the json. And then call the json serializer Serialize and Deserialize methods in the respective callbacks.