Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Nanako · Mar 29, 2015 at 05:12 PM · transformrecursionstackoverflow

Recursing through a bone hierarchy: Stack Overflow

My code is at the bottom of the post. The second function, FindChildByName is most important.

It takes a transform (a root bone of a skeleton) and a target name to search for. The idea is that it recurses through the hierarchy until it finds a bone with a matching name, or returns null.

As soon as i run it (without the print statement), unity freezes for approximately two seconds and then halts the script with a StackOverflowException

If i try to stick a debug message in there as shown below, it just freezes indefinitely, because print is very slow and its getting stuck in an infinite loop.

So i'm struggling to debug it. Where am i going wrong?

 void ProcessMeldPair(GameObject root, SkinnedMeshRenderer sub)
     {
         //creating a new object? i guess we're cloning the sub
         GameObject newObj = new GameObject(sub.gameObject.name);
         newObj.transform.parent = root.transform;
 
         //Cloning renderer
         SkinnedMeshRenderer newRenderer = newObj.AddComponent<SkinnedMeshRenderer>();
 
         //Assemble bone structure?
         Transform[] subBones = new Transform[sub.bones.Length];
         int i;
         for (i = 0; i < sub.bones.Length;i++)
         {
             subBones[i] = FindChildByName( sub.bones[ i ].name, root.transform );
         }
 
         //Assemble Renderer
         newRenderer.bones = subBones;
         newRenderer.sharedMesh = sub.sharedMesh;
         newRenderer.sharedMaterials = sub.sharedMaterials;
     }
 
     private Transform FindChildByName( string targetName, Transform bone )
     {
         print("Finding bone: " + targetName);
         Transform returnVar = null;    
         if (bone.name == targetName)
         {
             return bone;
         }
 
         foreach (Transform child in bone)
         {
             returnVar = FindChildByName(targetName, bone);
             if (returnVar)
             {
                 return returnVar;
             }
         }
         return null;
     }
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

1 Reply

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

Answer by maccabbe · Mar 29, 2015 at 05:28 PM

Line 35 should be

 returnVar = FindChildByName(targetName, child);

instead of

 returnVar = FindChildByName(targetName, bone);

Comment
Add comment · Show 3 · 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 Nanako · Mar 29, 2015 at 05:52 PM 0
Share

Are you sure? As far as i can tell, doing that will just repeatedly check the root bone of the hierarchy and go no deeper. Then there's no change from one iteration of the loop to the next.

i'm sure this won't crash, but i don't see how it will accomplish anything either.

avatar image maccabbe · Mar 29, 2015 at 06:10 PM 0
Share

Line 35 is currently

 returnVar = FindChildByName(targetName, bone);

Which means FindChildByName(targetName, bone) calls FindChildByName(targetName, bone) which then calls FindChildByName(targetName, bone) ad infinitum. Since the function keeps calling itself, more and more memory is being used until the memory stack overflows with information.

If you replace line 35 with

 returnVar = FindChildByName(targetName, child);

The new code will not repeatedly check the root bone but ins$$anonymous$$d check the children and therefore go as deep as it can until it returns.

avatar image Nanako · Mar 30, 2015 at 08:13 AM 0
Share

ooooohhh i got mixed up. normally epople would post "you should replace X with Y", so i have a habit of assu$$anonymous$$g the second option is your recommendation, and of not reading properly

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

20 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

Related Questions

Trying to Recreate Transform Child Heirarchy to a List 2 Answers

Huge Lists cause unity to throw bug reporter and crash? 0 Answers

transform.Find() returns a Transform? 1 Answer

Stack Overflow in if condition. 1 Answer

Stack overflow with recursive floodfill method. 2 Answers


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