- Home /
Serialization depth limit 7 exceeded during serialization
Hello,
I'm trying to create a combo system for my game, where each combo would be an array, with different characteristics for each attack in it. To create such combo system, I made the following class:
[System.Serializable]
public class AttackCharacteristics : IAttacks
{
//diffrerent values for each attack here
public AttackCharacteristics[] attacks;
public AttackCharacteristics(int _amount)
{
attacks = new AttackCharacteristics[_amount];
}
AttackCharacteristics[] IAttacks.attacks()
{
return attacks;
}
}
The problem is that, because I have
public AttackCharacteristics[] attacks;
it buids an infinite structure and I get an error "Serialization depth limit 7 exceeded...". I can't use [System.NonSerialized], because I want to manualy enter values for each attack in the inspector, and I can't find any other workaround to solve this error.
Thanks in advance for your answer.
Answer by kaarloew · Jan 08, 2019 at 07:17 AM
As a workaround, you can always use index instead of reference. So you have one big array for all AttackCharacteristics and in each AttackCharacteristic you have list of indexes that point to right AttackCharacteristic.
Unfortunately, I can't use this workaround, because in my script I have the following code:
private IAttacks[] combos = new IAttacks[2];
public AttackCharacteristics firstCombo = new AttackCharacteristics(3);
public AttackCharacteristics secondCombo = new AttackCharacteristics(3);
void Awake()
{
combos[0] = firstCombo;
combos[1] = secondCombo;
}
It allows me to later easely control all my combos. Hovewer, I can't add an array of AttackCharacteristics to IAttacks array. Or maybe I'm missing something?
It took me some time to figure out how to implement your workaround to my script, but in the end, your workaround helped me solve my issue, so thanks for correct answer :)
Answer by shatteredeyeinc · Jan 11, 2019 at 01:36 AM
you could user newtonsoftjson instead get the dll and put it into your plugins folder in unity and set ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
and use this instead of [System.Serializable]