Serialization depth limit 7 exceeded with linked-list-like data structure

I’m working on a custom editor window that has a node that connects to another node and so on.

I save and load from a file with serialization(system.serializable) and I don’t have any problem with this part until now.

The problem came up when I made a recursive function to draw lines between nodes. This recursion loops through a list of next nodes from a node to draw lines and continue until reaches null terminations.
The warning message came up in Unity like this :


Serialization depth limit 7 exceeded at ‘~::~.<~~~>k__BackingField’. There may be an object composition cycle in one or more of your serialized classes.

Serialization hierarchy:
8: ~.~::~.k__BackingField
7: ~.~::
~.k__BackingField
6: ~.~::~.NextNodes
5: ~.~::
~.NextNodes
4: ~.~::~.NextNodes
3: ~.~::
~.NextNodes
2: ~.~::~.NextNodes
1: ~.~::
~.NextNodes
0: ~.~::~~~.StartingNode


My question is what does this warning mean?

Can I ignore this warning? Will there be a problem in recursion itself?

Thank you in advance.

The problem is that Unity’s serializer cannot handle potentially infinitely deep data. Imagine something like this:

[Serializable]
class Node
{
    [SerializeField]
    List<Node> m_Children = new List<Node>();
}

Each of those children could keep having children, so Unity enforces an artificial serialization depth limit of 7. This warning is basically telling you that your data layout is not robust.

The way to work around it is to instead serialize some kind of identifier. For example:

[Serializable]
class Node
{
    [SerializeField]
    int m_ID;
    [SerializeField]
    List<int> m_ChildrenIDs = new List<int>();
}

But at all, if you make this workaround, the situation is the same, endless recursion can be made. Recursion for a node system is normal. I would recommend Unity developers to remove the warnings, or let us supress the warning in Unity settings and let give us a recursion limit. If this will be exceeded runtime, then a warning or error is appropriate. At least, let us supress the warning. I can remember me having similar issue in Java for a node system, where the call-stack buffer grew over 512 MB, but this was okay, it was necessary.

For future reference you can use Odin Serializer:

http://www.sirenix.net/odininspector/manual/introduction/serialize-anything

Look at “Avoiding Unity’s Infinite Depth Warning”

The one hangup is all your classes must extend from a SerializedScriptableObject which can be annoying for classes that are simple primitives and you now have to create extra objects for them. But its a better trade off to actually get rid of this issue.