Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Class loop error

Discussion in 'Scripting' started by DarkX, Oct 15, 2015.

  1. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    hey guys,

    i am making a talk system,

    how i can make a loop class display on inspector without using [system.serializable] because happen this error:
    Serialization depth limit exceeded at 'PlayerTalkLine'. There may be an object composition cycle in one or more of your serialized classes.
    script
    Code (csharp):
    1.  
    2.  
    3. [System.Serializable]
    4. public class TalkLine{
    5.     public string DiagLine = "";
    6.     public bool IsPlayer = false;
    7.     public AudioClip _Audio = new AudioClip();
    8.     public bool SendMensage = false;
    9.     public string MensageName = "";
    10.     public PlayerTalkLine[] OtherLines = new PlayerTalkLine[0];
    11. }
    12. [System.Serializable]
    13. public class PlayerTalkLine{
    14.     public string DiagLine = "";
    15.     public bool IsPlayer = false;
    16.     public AudioClip _Audio = new AudioClip();
    17.     public bool SendMensage = false;
    18.     public string MensageName = "";
    19.     public TalkLine OtherLines = new TalkLine();
    20. }
    21.  
    22.  
    thanks in advance
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Basically, this is an issue when you have two classes that reference each other. Unity's serialization isn't smart enough to know that if A points to B and B points to A that these are the same thing; it serializes it believing that A is a child of B is a child of A is a child of B.... it only goes so many levels deep like this, and then it throws up that error message. (IIRC, it's also not smart enough to know that it doesn't need to keep digging into a null object, but I don't think that's what's happening here)

    The solution that I've found (admittedly it's kind of a pain) is to give each object of one of the classes a unique ID, have each one register itself into a static list or dictionary, and instead of linking to that class, store its ID. You can have a direct reference as a public member or property, but you must use [System.NonSerialized] to make Unity skip over it. I recommend making this a property that, if the cached reference is null, goes to the dictionary and looks up its reference. That way, any code outside these classes won't have to be changed.
     
    DarkX likes this.
  3. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    thanks, but are working without lag, therefore i will not modify nothing,
    because if i edit, i will have to make everything again(all the dialogues)