Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 giantkilleroverunity3d · Feb 05, 2019 at 09:40 PM · prefabsgameobject.findgetcomponentinchildrentransform.getcomponentsinchildren

How do I retrieve deep levels of nested child transforms with a generic snippet of code

This current project exists in Unity 2018.2.2f1.
I want to loop through the children of children of children of a root parent to execute a method of the object/transform at the 4th level of a parent/child ladder.
I have gone about this for two days now. Most answers are posted for gameobjects which the children are not. They are transforms. So for anybody reading this get this point straight before any comments or posts. I understand the concept, at least this is what is drilled in by those dealing with multi levels of children. I can not seem to get to 4th level of children. I suspect my Transform designations are a mess, but I cant seem to find a straight forward generic code snippet for this. I believe I am mixing gameobjects with transforms with arrays. If my code is terribly wrong then so be it. It is a mishmash of multiple posts of attempts. I have done due diligence in many , many runs of code to no avail. If my grammar incorrect excuse you and show me and the future readers kindness and mercy. Please no techno babble, condensending remarks or toss in comments. Only revelance here. This answer is not just for me but for future readers who might not be well versed in Unity or C, GameObject manipulations. The last item mentioned is the wall that all new developers come up against when their game size exceeds their forte(surprise). I am asking for an expert touch so that in the future any body coming along will be able to run through your solution and add any nest hierarchy and display a stream of debug.log line of any parent child nesting/depth. This is missing from all the posts I have seen. I have consigned myself to stick with strictly Transforms but am open to gameobject usage as well. I just want to stop wasting time going round and round reading 2 level parent/child answers. The Rgo gameobject is where it starts and I have access to the methods and properties. Lets go deeper. I am really trying to execute a script method in 4th level gameobject (TSPos and TSNeg).
Attached is a unity run screen that shows the hierarchy without me exposing any data as expressed in my Non-Disclosure Agreement with my contracting lead. alt text As can be seen there is a child(tsChild) of a child(child) at the lower end of the relation that display the same name. tsChild is supposed to be a child of 'child' but I get the same item.

I am not trying get scolded by authoritarians if this post deems unacceptable. Supply a solution that we can all use and dispel with the ruler across the knuckles diatribe for the sake of the sanctity of the community. I have read the docos and posts ad infinitum. I have also run up against attempts in Unity that have gotten the final statement 'Sorry for the inconvenience' or 'We missed that one' or 'this method has been deprecated' only to have it resurface in a future update.
Thank you all. I hope I have supplied enough information about what I have come up against and what the community need is here.
Now I can go take a mandatory shower and eat correctly before I smell like food to my dogs.

2019-02-04-21-01-30-windowdualtickergenerationatte.png (28.0 kB)
Comment
Add comment · Show 1
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 xxmariofer · Feb 05, 2019 at 10:14 PM 0
Share

hello, you didnt attach any code, or i cant see it. second you want to retrieve all the childs of the object? and the childs of the childs of the childs? all in just one array? or i am missing something? whats your exact need? you need to save it by its level of parent? or all in one list/array?

2 Replies

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

Answer by Ymrasu · Feb 05, 2019 at 10:25 PM

This is done through GetChild method of transform. Transforms act as arrays that hold child transforms. The index of the children are their placements within the Hierarchy.

 // We can use a 'current' transform to help transverse
         Transform current = transform; // current transform is Ring Gen
         Debug.Log(current.name);
         current = current.GetChild(0); // current transform is now RingPair (0)
         Debug.Log(current.name);
         current = current.GetChild(0); // current transform is now TSPair (0)
         Debug.Log(current.name);
         current = current.GetChild(0); // current transform is now TSPos
         Debug.Log(current.name);
 
         // OR we can chain the getchild calls
 
         // transform is parent or level 1, then chain 3 getchild to get to level 4
         current = transform.GetChild(0).GetChild(0).GetChild(1); // last GetChild use index 1 for the second child (TSNeg)
         Debug.Log(current.name);

alt text


capture.png (17.0 kB)
Comment
Add comment · Show 16 · 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 Ymrasu · Feb 05, 2019 at 10:34 PM 1
Share

Or if you just want to loop through all the children you can set up a foreach loop:

 void Start()
     {
         DisplayChildren(transform);
     }
 
     void DisplayChildren(Transform trans)
     {
         foreach(Transform child in trans) {
             Debug.Log(child.name);
             if (child.childCount > 0) {
                 DisplayChildren(child);
             }
         }
     }

this loops through the children of the provided transform, then if that transform has children it calls itself to loop through those children as so forth.

avatar image xxmariofer Ymrasu · Feb 05, 2019 at 10:41 PM 1
Share

hello, your code will not work altought i think is closer to what he is searching, you need to change the foreach for a classic for loop :)

  int childCount = parent.childCount;

  for(int iterator = 0; iterator < childCount; iterator++)
  {
      DisplayChildren(trans.GetChild(iterator));
  }
avatar image Ymrasu xxmariofer · Feb 05, 2019 at 11:23 PM 0
Share

foreach loops work on transforms to get the children of the transform

Show more comments
Show more comments
avatar image giantkilleroverunity3d · Feb 06, 2019 at 03:39 AM 0
Share

@Ymrasu Yes Neg and Pos during runtime from an instantiated gameobject later. I am writing down the firing sequence of the whole instantiation event. $$anonymous$$ight the problem lie in the script execution sequence? Are run time instantiated from runtime instantiated left till last in the execution sequence? Will profiler show this?

avatar image giantkilleroverunity3d · Feb 06, 2019 at 04:45 AM 0
Share

The initial pairs are instantiated from a script on parent prefab which is instantiated by the ring. I believe this thread could be closed at this time as I have the recursive routine that produces very clear results. The instantiated tsPairs are built from a single prefab. The underlying children are instantiated from a GO that is transform and a script component. The script component uses the tsPairs prefab. I now have to get the hierarchy level that I want selected so execute the method.

avatar image giantkilleroverunity3d · Feb 06, 2019 at 04:58 AM 1
Share

@Ymrasu & @xxmariofer Problem solved!
Thank you to both of you for getting me passed this point.
Your efforts pointed out something. I had the NegPos instantiations in the the start().
I had to move those to a separate method in the script and call that after the main prefab instantiation. When there is a prefab of prefabs the start() code always executes in a non compliant order. If there is a way to control this I would like to know. Else you call the method right after instantiation and you control the order. Had I seen this escapee before I would not have had to post this question.
But now we are all the smarter. @xxmariofer : I tried the for loop and eventually settled on what @Ymrasu supplied. I also used the secondary Transform selection that @Ymrasu supplied to copy the transform to the method parameter.
Now the recursiveness creates a completely generic routine no matter how large a Prefab is. @Ymrasu If you post the answer I will accept. Thanks again.

And may the future Unitarians see this and have it solve their request.

Here is the code I added:

     Transform current = transform; // current transform is Ring Gen
      DisplayChildren(current);// Recursive and grabs everything.
 

         
avatar image
0

Answer by thinkblue · Jan 15 at 09:25 PM

 // global class variable
 Transform childFound = null;
 
 Transform CustomFindChild(string key, Transform parent)
     {
         foreach (Transform child in parent)
         {
             if (child.name == key)
             {
                 childFound = child;
             } else
             {
                 if (child.childCount > 0)
                 {
                     if (childFound == null)
                     {
                         CustomFindChild(key, child);
                     }
                 }
             }
         }
 
         return childFound;
     }
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

104 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 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

GetComponentInChildren is getting Sibling s Child 0 Answers

Text Not Being Displayed above Instantiated Prefabs 0 Answers

How to instantiate a player's weapon after picking it up? 1 Answer

Problems with finding all children that have meshes 2 Answers

Problem with player prefab 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