Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  5. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  6. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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

Discussion in 'Extensions & OnGUI' started by elmar1028, May 4, 2018.

  1. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,014
    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:
    8,116
    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,014
    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:
    8,116
    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.   m_GameObject: {fileID: 1098847255}
    2.   m_Script: {fileID: 11500000, guid: 973aa9ab4e80482458201283bac1ca8d, type: 3}
    3.     windowSettings:
    4.       appearanceSettings:
    5.         positionSettings:
    6.           alignmentSettings:
    7.             alignmentInfo:
    8.               rects:
    9.               - serializedVersion: 2
    10.                 x: 0
    11.                 y: 0
    12.                 width: 0
    13.                 height: 0
     
    elmar1028 likes this.
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,014
    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.   m_ObjectHideFlags: 0
    2.   m_PrefabParentObject: {fileID: 0}
    3.   m_PrefabInternal: {fileID: 0}
    4.   m_GameObject: {fileID: 105898369}
    5.   m_Enabled: 1
    6.   m_EditorHideFlags: 0
    7.   m_Script: {fileID: 11500000, guid: 1d695788e62876c42ac3102eb1dbb5b1, type: 3}
    8.   m_Name:
    9.   m_EditorClassIdentifier:
    10.   depth1:
    11.     depth2:
    12.       depth3:
    13.         depth4:
    14.           depth5:
    15.             depth6:
    16.               depth7:
    17.                 depth8:
    18.                   content:
    19.                 content:
    20.               content:
    21.             content:
    22.           content:
    23.         content:
    24.       content:
    25.     content:
    26.   content:
    27. --- !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.