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

Serialization depth limit exceeded

Discussion in 'Scripting' started by DarkX, Jan 4, 2016.

  1. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    Hey guys, i am making a game with choices, like an RPG talk system

    but i can make more than 7 lines because the unity serialize only seven

    error:Serialization depth limit exceeded

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [System.Serializable]
    6. public class class_a{
    7.     public string talk;
    8.     public class_a[] _a;
    9. }
    10.  
    11. public class DialogList : MonoBehaviour {
    12.     public class_a depth;
    13. }
    14.  
    15.  
    note: cant be with nonserializable because i have to edit in the inspector
    note 2 : Unity 5

    thanks in advance and happy holidays
     
    Last edited: Jan 5, 2016
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    unity's serialization system for non-unity types is not referenced based, but value base.

    So if you have a class reference an object of the same type (or an object, that references an object of the same type), the class_a in it will be serialized by value.

    The result is recursive serialization... infinite loop.

    class_a serializes a class_a which serializes a class_a which serializes a class_a which serializes a class_a........ ad infinum


    If you're trying to make a link-list like this, you should not serialize the node links, and instead break the entire list out into an array for serialization.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Unity's serializer is not exceptionally bright when it comes to nested classes. It basically doesn't understand the concept of 'null', nor really of 'objects'. Your actual data might look like this:

    Code (csharp):
    1. depth -> _a -> null
    But Unity's serializer doesn't see it that way. It will see:
    Code (csharp):
    1. depth -> _a -> _a=null -> _a=null -> _a=null.....
    Because it just doesn't stop when _a is null, it expands everything that that class could have in it. And because your class is recursive, that it could be infinite. This "7 layers" message is Unity's own cutoff, to prevent the serializer from getting into an infinite loop.

    The only solution I'm aware of is to simply not use Unity's serializer in situations like this. You could, for example, serialize with JSON instead, which I believe will not have this problem.
     
  4. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    yep, but will not occur if i put in some place one array with a length 0, how i can fix this?
     
  5. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    StarManta how i can use JSON ?
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    Our company uses SimpleJSON (well, a modification of it), but we've been using that for a long time, and since then Unity 5.3 introduced native JSON support, though I haven't experimented with it.
     
  7. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    StarManta i downloaded but how i can fix the problem,

    like

    [UseJSONSerializer]
    public class_a[] code

    ?
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    You'll have to perform the serialization yourself... most common technique to do it directly in a script is to use ISerializationCallbackReceiver:
    http://docs.unity3d.com/ScriptReference/ISerializationCallbackReceiver.html

    The 2 methods are called before and after serialization. You would mark the field/var of your script as [System.NonSerialized()]. And then a private field marked as [SerializedField()] that stores the json. And then call the json serializer Serialize and Deserialize methods in the respective callbacks.
     
    DarkX likes this.
  9. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    please examples :p
     
  10. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42
    bump
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    There are code samples for both of those JSON readers on their respective pages.
     
    DarkX likes this.
  12. DarkX

    DarkX

    Joined:
    Apr 6, 2014
    Posts:
    42