Serialization depth limit exceeded with a private field.

I know there are many topics on this subject. However, it did not work for me. Unity seems to be attempting to serialize private variables.

Here is the full error message.

"Serialization depth limit 7 exceeded at ‘VinhNguyen.Characters.Attributies::Attribute.linkers’. There may be an object composition cycle in one or more of your serialized classes.
Serialization hierarchy:

8: VinhNguyen.Characters.Attributies::Attribute.linkers

7: VinhNguyen.Characters.Attributies.Linkers::Linker.attribute

6: VinhNguyen.Characters.Attributies::Attribute.linkers

5: VinhNguyen.Characters.Attributies.Linkers::Linker.attribute

4: VinhNguyen.Characters.Attributies::Attribute.linkers"

   [System.Serializable]
    public class Attribute
    {
        #region Fields

        [Header("Base")]

        [SerializeField]
        private AttributeType type = AttributeType.None;

        [SerializeField]
        private float baseValue = 0;

        [Header("Level")]

        [SerializeField]
        private int level = 0;

        [SerializeField]
        private float bonusValuePerLevel = 0;

        [SerializeField]
        private List<Linker> linkers = new List<Linker>();

        private List<Modifier> modifiers = new List<Modifier>();

        [SerializeField]
        private float currentValue = 0;

        [SerializeField]
        private float value = 0;

        private bool changed = true;
        private AttributeOptionsData optionsData;
        public AttributeDictionary AttributeDictionary { get; private set; }

        public delegate void AttributeEventHandler(Attribute attribute);

        public event AttributeEventHandler OnValueChanged;

        #endregion
   }

   [System.Serializable]
    public class Linker
    {
        #region Fields

        [SerializeField]
        private AttributeType linkedAttributeType;

        [SerializeField]
        private float ratioValue;

        public delegate void LinkerEventHandler(Linker linker);

        public event LinkerEventHandler OnValueChanged;
        
        private Attribute attribute;
        private Attribute linkedAttribute;

        #endregion
   }

That’s probably because you declared Attribute class as Serializable, so the compiler tries to serialize it for Linker class (even if you’re not declaring the private member “attribute” as SerializeFields, remember that you can change the view mode of Inspector to Debug to see the private fields) and this is creating a loop in the serialization process.

An easy workaround could be for example to have a string/number unique identifier in Attribute class and store it also in Linker class instead of a direct reference to the Attribute object. Also you wouldn’t need linkedAttributeType in Linker class, because you should be able to get it from linked attribute (searched by a method instead of referenced in a property).