- Home /
A work around for Serialization depth limit ?
Hello, i have the warning message "Serialization depth limit 7 exceeded at 'MyClass.myList'. There may be an object composition cycle in one or more of your serialized classes" in my script. I know why it does that but i would like to know how i can't avoid this error. Some people advice to make a list of an integrer or a string to refer indirectly to my class, but i don't know how to do that exaclty. Here is the script i want to make :
[System.Serializable]
public class someGuy
{
public string hisName;
public List<someGuy> hisEnnemies = new List<someGuy>();
}
myFunction() {
string string1 = someGuy3.hisName;
string string2 = someGuy3.hisEnnemies[Random.Range(0, someGuy3.hisEnnemies.Count)].hisName;
Debug.Log (string2 + " is an ennemy of " + string1);
}
Thanks !
Answer by EternalClickbait · Jul 02, 2019 at 05:51 AM
The problem is that every SomeGuy
instance creates another list of SomeGuy
instances, which creates another and so on. This will go on forever until you get a stack overflow. Unless you really need to, get rid of the enemies list.
$$anonymous$$any people give solutions to work around this with string or int, but i don't understand it as i don't know how to use these string/int after that...
https://answers.unity.com/questions/1393470/serialization-depth-limit-7-exceeded-with-linked-l.html
To retrieve SomeGuy element, you'll need to store all SomeGuys and then you can follow your link steps. $$anonymous$$aybe you could store just the **SomeGuy**s you need using ISerializationCallbackReceiver.h
Your answer
Follow this Question
Related Questions
Custom Class Array Serialization Problem 0 Answers
class design preference 1 Answer
How should I declare a nested class to find it latter with SerializedObject.FindProperty? 1 Answer
do Static classes get serialized? 0 Answers
Creating a class? 2 Answers