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
2
Question by BerggreenDK · Jul 19, 2011 at 10:35 PM · runtimehierarchyroot

How to retrieve current shown hierarchy?

Lets say I dont know ANY of the names of ANY of the current objects in the scene right now. Ofcause I know at least one, the one where this script will be attached.

But, there are multiple hierachies active at the same time and I want the user to be able to post a debug back to our server later.

They are all initiated from different scripts, so there is no global list of my own.

How can I trace/debug.log all objects? what collection or how do I list the objects in Hierarchy right now.

I want to make a "List all" button that goes through them all and print their names with Debug.Log().

Of cause if everything is inside a single "root" then I could just find childrens, but there isnt or is there a secret/hidden root object in unity3D ?

This is what I've got so far:

     void DebugRoot()
 {
     Debug.Log("----- Debug root -----");
     Transform trans = transform.root;
     DebugDeeper(trans);
     
 }
 
 void DebugDeeper(Transform trans)
 {
     int children = trans.GetChildCount();
     Debug.Log(trans.name + " has " + children);
     
     for (int child=0;child<children;child++)
     {
         DebugDeeper( trans.GetChild(child) );
     }
 }

It prints the hierachy below the object where I attach the script, but there are more root elements within the hierachy, so I need to ajust transform.root somehow or find a list of root(s) ???

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

2 Replies

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

Answer by BerggreenDK · Jul 19, 2011 at 11:19 PM

aha! Got it! posting this as the answer for everyone else. Please let me know if you can use it too. Then I might post more such scripts in the future.

 void DebugRoot()
 {
     Debug.Log("----- Debug root -----");
     foreach (GameObject go in Object.FindObjectsOfType(typeof(GameObject)))
     {
         if (go.transform.parent == null)
         {
             DebugDeeper(go.transform);
         }
     }
 }
 
 void DebugDeeper(Transform trans)
 {
     int children = trans.GetChildCount();
     Debug.Log(trans.name + " has " + children);
     
     for (int child=0;child<children;child++)
     {
         DebugDeeper( trans.GetChild(child) );
     }
 }

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
avatar image
2

Answer by idbrii · Aug 14, 2020 at 12:05 AM

Similar answer but instead of Object.FindObjectsOfType(typeof(GameObject)), use SceneManager.GetActiveScene().GetRootGameObjects() so you only look at each object once. (FindObjectsOfType should find every object. GetRootGameObjects only gives the top-level ones so we can print them with their children.)

 [UnityEditor.MenuItem("GameObject/PrintHierarchy")]
 static void PrintHierarchy()
 {
     Debug.Log("Print object hierarchy");
     foreach (var obj in SceneManager.GetActiveScene().GetRootGameObjects())
     {
         Debug.Log("Root object");
         PrintChildren(obj.transform, "");
     }
 }
 
 static void PrintChildren(Transform t, string indent)
 {
     int child_count = t.childCount;
     Debug.Log($"{indent}'{t.name}' has {child_count} children");
 
     var more_indent = indent + "\t";
     for (int i = 0; i < child_count; ++i)
     {
         var child = t.GetChild(i);
         PrintChildren(child, more_indent);
     }
 }
 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Export objects to a .3DS file at runtime 1 Answer

Is it possible to get all root transforms of the scene hierarchy? 2 Answers

Objects not updating if not selected in Hierarchy? 0 Answers

Dynamically parenting a collider object to a rigidbody object causes collision bug. 1 Answer

Why does hierarchy order change at runtime? 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