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
3
Question by Johan 4 · Feb 23, 2011 at 04:12 PM · editorhierarchy

Get all items from hierarchy?

In editor how can I get a list of all items in the hierarchy and possibly refer to each one?

For example create a list from all objects in a text file. I know the text part, but not the get hierarchy list.

Thanks

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
11
Best Answer

Answer by yoyo · Feb 23, 2011 at 04:32 PM

You can find all game objects like this:

foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
{
    Debug.Log(obj.name);
}

If you want to traverse down the hierarchy from a specific object, do something like this:

void Traverse(GameObject obj) { Debug.Log(obj.name); foreach (Transform child in obj.transform) { Traverse(child.gameObject); }

}

To traverse the whole scene from the root, combine the above into something like this:

foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
{
    if (obj.transform.parent == null)
    {
        Traverse(obj);
    }
}
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 ProbePLayer · Oct 06, 2013 at 06:23 PM 0
Share

sweet, saved my life with that one mate. thanks

avatar image PlanetTimmy · Jul 20, 2015 at 06:03 PM 1
Share

Note that as of Unity 4.6 FindObjectsOfType will not include inactive GameObjects.

In my experimentation I've also found this method misses out some active objects, which is rather odd. I wonder if it might be something to do with the hideflags.

avatar image Siliproksi · Aug 29, 2016 at 01:00 PM 0
Share

What if obj is object that has child's.So it makes it array.And than i want to foreach that obj.But it would say "foreach statement cannot operate on variables of type UnityEngine.GameObject' because it does not contain a definition for GetEnumerator' or is not accessible".Because that object is not defined as an array.But if you say "foreach (GameObject[] obj)" than you cant get objects that are not array sadly :( Please help on this one !

avatar image ProbePLayer · Aug 29, 2016 at 04:42 PM 1
Share

@Siliproksi I tested this code mate, it works on all levels. here, does your code look like this?

 void Traverse(GameObject obj)
         {
             Debug.Log (obj.name);
             foreach (Transform child in obj.transform) {
                 Traverse (child.gameObject);
             }
     
         }
     
         // Use this for initialization
         void Start () {
             foreach (GameObject obj in Object.FindObjectsOfType(typeof(GameObject)))
             {
                 if (obj.transform.parent == null)
                 {
                     Traverse(obj);
                 }
             }
         }
 
 
avatar image
0

Answer by Siliproksi · Aug 30, 2016 at 01:58 PM

@ProbePlayer Oh sorry i didnt saw that. No my code is not like that.But i am pretty sure it works like that.I didnt saw that Traverse function you mentioned.Anyways i found that i dont even need it xD :) But yea this is great way to do it.Thanks :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Gibt es eine Lösung/Einstellung für: Hierarchy Custom / My Sort? 1 Answer

Object not selected in hierarchy gives an error 0 Answers

How to show an icon in Hierarchy view 5 Answers

Detect on Editor Mode drop prefab to the hierarchy 1 Answer

Overwrite the inspector window on Scene Asset heading chosen in the hierarchy window 0 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