Serialization Problems with Array of the same Object

Hi guys, I want to serialize ad Array of Array of the same objects.

I have this Base class:

[System.Serializable]
public class Item
{
    public ItemChildren children;
}

that have this class for the children:

[System.Serializable]
public class ItemChildren : ScriptableObject
{
    public List<Item> list;
}

If I run the the game all seems to be ok, but if I close and open unity again, I have a lot of error.
In fact, Unity don’t save the array of children…

How to achieve this??

When the serializer serializes classes that aren’t a UnityEngine.Object (basically, not GameObject, Component, or the ilk). The serializer doesn’t actually save references… it makes brand new copies of those objects.

So your code there ends up with an endlessly recursing dependency structure.

Item has an ItemChildren that has Items that has ItemChildren that has Items that has ItemChildren… on and on and on.

If you have some sort of tree structure (which it seems like you have), the best thing would be to not have the tree be serializeable, but instead use the ISerializationCallbackReceiver interface to serialize/deserialize the tree into a flat structure that Unity can handle.

Yes, I have a Tree with infinite children…
Do you have any example?

Sure.

Thx! I have achieved this with a List of int where the int value is the key of the object in the dictionary!
And the dictionary is serialized like this example!

For now it work like a sharm :smile:

Thank you!