Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by gusrbehfdl · Aug 14, 2017 at 09:15 AM · serializationrecursion

Serialization depth limit 7 exceeded with linked-list-like data structure

I'm working on a custom editor window that has a node that connects to another node and so on.

I save and load from a file with serialization(system.serializable) and I don't have any problem with this part until now.

The problem came up when I made a recursive function to draw lines between nodes. This recursion loops through a list of next nodes from a node to draw lines and continue until reaches null terminations. The warning message came up in Unity like this :


Serialization depth limit 7 exceeded at '~~~::~~~.<~~~>k__BackingField'. There may be an object composition cycle in one or more of your serialized classes.

Serialization hierarchy: 8: ~~~.~~~::~~~.k_BackingField 7: ~~~.~~~::~~~.k_BackingField 6: ~~~.~~~::~~~.NextNodes 5: ~~~.~~~::~~~.NextNodes 4: ~~~.~~~::~~~.NextNodes 3: ~~~.~~~::~~~.NextNodes 2: ~~~.~~~::~~~.NextNodes 1: ~~~.~~~::~~~.NextNodes 0: ~~~.~~~::~~~.StartingNode


My question is what does this warning mean?

Can I ignore this warning? Will there be a problem in recursion itself?

Thank you in advance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Adam-Mechtley · Aug 14, 2017 at 09:22 AM

The problem is that Unity's serializer cannot handle potentially infinitely deep data. Imagine something like this:

 [Serializable]
 class Node
 {
     [SerializeField]
     List<Node> m_Children = new List<Node>();
 }

Each of those children could keep having children, so Unity enforces an artificial serialization depth limit of 7. This warning is basically telling you that your data layout is not robust.

The way to work around it is to instead serialize some kind of identifier. For example:

 [Serializable]
 class Node
 {
     [SerializeField]
     int m_ID;
     [SerializeField]
     List<int> m_ChildrenIDs = new List<int>();
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gusrbehfdl · Aug 14, 2017 at 01:05 PM 0
Share

Thank you. I did exactly that, using ID ins$$anonymous$$d of referencing classes, and warning message is gone. It gives me a question, though. Why did warning came up only when I used my data structure in a recursion and not before? Also my recursive method doesn't serialize or deserialize(save&load from db). It just gets values to draw lines. Shouldn't this be irrelevant to serialization?

avatar image Adam-Mechtley gusrbehfdl · Aug 14, 2017 at 05:34 PM 1
Share

There are a lot of factors at play so it is hard to say without knowing more details. I suggest referring to the documentation page on script serialization. For example, if you also added some private Node or List<Node> field on your EditorWindow, it would be subject to this rule:

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.

avatar image gusrbehfdl Adam-Mechtley · Aug 14, 2017 at 06:37 PM 0
Share

I've read that before but your explanation made me better understand it now. I somehow thought Unity had nothing to do with it. Thank you again. This has been a good study.

Show more comments
avatar image
3

Answer by mkgame · Nov 06, 2017 at 05:30 PM

But at all, if you make this workaround, the situation is the same, endless recursion can be made. Recursion for a node system is normal. I would recommend Unity developers to remove the warnings, or let us supress the warning in Unity settings and let give us a recursion limit. If this will be exceeded runtime, then a warning or error is appropriate. At least, let us supress the warning. I can remember me having similar issue in Java for a node system, where the call-stack buffer grew over 512 MB, but this was okay, it was necessary.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Nov 06, 2017 at 06:37 PM 2
Share

That's not possible. Unity's serialization system need to know the size of the serialized data ahead of time. That's the main reason why it doesn't support polymorphism and doesn't allow recursive structures to be serialized. In the beginning a structure like this had actually crashed Unity. Since then they implemented this sanity check. When they first implemented this limit there was no warning and tons of people complained that it simply stops working without any notice. Now it always displays the warning as you actually did something that isn't supported.


Don't get me wrong. There's no problem creating a node based recursive system in Unity. You just can't make it serializable.


I suggest you read the script serialization documentation carefully. You may also read through this forum thread.


If you need serializable nodes you have to do cross references like $$anonymous$$showed in his answer (by using IDs ins$$anonymous$$d of references). This way you have a single array with all nodes. Another way is to actually use either a $$anonymous$$onoBehaviour or ScriptableObject derived class as "Node". Those are / can be serialized as seperate assets and cross references are actually serialized correctly.

avatar image
-2

Answer by Aggressor · May 13, 2018 at 03:01 PM

For future reference you can use Odin Serializer:

http://www.sirenix.net/odininspector/manual/introduction/serialize-anything

Look at "Avoiding Unity's Infinite Depth Warning"

The one hangup is all your classes must extend from a SerializedScriptableObject which can be annoying for classes that are simple primitives and you now have to create extra objects for them. But its a better trade off to actually get rid of this issue.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Recursive serialization manual limit 1 Answer

Serializing class that has List of members of same type and referencing them in scene causes awfull long assembly reload time 0 Answers

Why does the editor crash whenever I select a certain script in the project or object with the same script attached in the hierarchy? 2 Answers

Can a GameObject be serialized? 1 Answer

Detecting serialization reload in editor 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges