Search Unity

Getting a "Serialization depth limit 7..." error for no reason!

Discussion in 'Immediate Mode GUI (IMGUI)' started by elmar1028, May 4, 2018.

  1. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    I've seen people getting such error due to circular reference but that's not the case for me. I am getting the following error.

    This is how PositionTransform looks like:

    Code (CSharp):
    1. namespace MyNamespace
    2. {
    3.     [Serializable]
    4.     public class Settings
    5.     {
    6.         public Vector3 PositionOffset;
    7.  
    8.         // unity says this hits the serialization limit
    9.         public RectTransform PositionTransform;
    10.  
    11.         public RectTransform UITransformTarget;
    12.      
    13.         //etc...
    14.     }
    15. }
    I also tried changing the variable type of PositionTrasnform (to GameObject) but it would output exact error.

    Any ideas?
     
    Last edited: May 7, 2018
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    I'll assume Settings has a [Serializable] attribute.

    Does it contain a private Settings variable?

    From Manual: Script Serialization:

    When reloading scripts, Unity restores all variables - including private variables - that fulfill the requirements for serialization, even if a variable has no SerializeField attribute. In some cases, you specifically need to prevent private variables from being restored: For example, if you want a reference to be null after reloading from scripts. In this case, use the NonSerializable attribute.
    For an example, see this post.

    If not, then you may simply have nested your data too deep.
     
    elmar1028 likes this.
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    Yes. I forgot to add it there. Updated the post.

    Nope. I don't have Settings variable inside.

    Yeah, quite possibly I've hit the limit by manual work (not due to loops). It's quite strange that Unity settled with a limit of 7. I will just redo the whole thing from scratch and see where I got it wrong.
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    It might help to look at your existing code and count the levels to be sure. Or set your project's serialization to Force Text and look at the YAML. For example, this is 7 levels already:

    Code (csharp):
    1. MonoBehaviour:
    2.   m_GameObject: {fileID: 1098847255}
    3.   m_Script: {fileID: 11500000, guid: 973aa9ab4e80482458201283bac1ca8d, type: 3}
    4.   settings:
    5.     windowSettings:
    6.       appearanceSettings:
    7.         positionSettings:
    8.           alignmentSettings:
    9.             alignmentInfo:
    10.               rects:
    11.               - serializedVersion: 2
    12.                 x: 0
    13.                 y: 0
    14.                 width: 0
    15.                 height: 0
     
    elmar1028 likes this.
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,359
    That's something I didn't even think of checking! Thanks a lot for this. It would be much easier to keep hierarchy in check.

    Settings class doesn't seem to break the limit for me.

    I did a small test to see if I could break the limit like that.

    I wrote this code:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. public class DepthTest : MonoBehaviour
    5. {
    6.     [SerializeField] public Depth1 depth1;
    7.     public string content;
    8. }
    9.  
    10. [Serializable]
    11. public class Depth1
    12. {
    13.     [SerializeField] public Depth2 depth2;
    14.     public string content;
    15. }
    16.  
    17. [Serializable]
    18. public class Depth2
    19. {
    20.     [SerializeField] public Depth3 depth3;
    21.     public string content;
    22. }
    23.  
    24. [Serializable]
    25. public class Depth3
    26. {
    27.     [SerializeField] public Depth4 depth4;
    28.     public string content;
    29. }
    30.  
    31. [Serializable]
    32. public class Depth4
    33. {
    34.     [SerializeField] public Depth5 depth5;
    35.     public string content;
    36. }
    37.  
    38. [Serializable]
    39. public class Depth5
    40. {
    41.     [SerializeField] public Depth6 depth6;
    42.     public string content;
    43. }
    44.  
    45. [Serializable]
    46. public class Depth6
    47. {
    48.     [SerializeField] public Depth7 depth7;
    49.     public string content;
    50. }
    51.  
    52. [Serializable]
    53. public class Depth7
    54. {
    55.     [SerializeField] public Depth8 depth8;
    56.     public string content;
    57. }
    58.  
    59. [Serializable]
    60. public class Depth8
    61. {
    62.     public string content;
    63. }
    Attached to GameObject in Unity Scene and get this:

    depth test.png

    This is how it shows up in YAML:

    Code (CSharp):
    1. MonoBehaviour:
    2.   m_ObjectHideFlags: 0
    3.   m_PrefabParentObject: {fileID: 0}
    4.   m_PrefabInternal: {fileID: 0}
    5.   m_GameObject: {fileID: 105898369}
    6.   m_Enabled: 1
    7.   m_EditorHideFlags: 0
    8.   m_Script: {fileID: 11500000, guid: 1d695788e62876c42ac3102eb1dbb5b1, type: 3}
    9.   m_Name:
    10.   m_EditorClassIdentifier:
    11.   depth1:
    12.     depth2:
    13.       depth3:
    14.         depth4:
    15.           depth5:
    16.             depth6:
    17.               depth7:
    18.                 depth8:
    19.                   content:
    20.                 content:
    21.               content:
    22.             content:
    23.           content:
    24.         content:
    25.       content:
    26.     content:
    27.   content:
    28. --- !u!4 &105898371
    Unity doesn't throw any errors on that matter. Or am I misunderstanding something?

    Regardless, I will keep a close eye on errors like these as I rebuild everything.
     
  6. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    429
    If you add depth9 or depth 10 it will through warning.