• 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 IMTRIGGERHAPPY9 · Aug 15, 2013 at 11:42 PM · c#parentchildchildrenforeach

Get Children of Child and Deactivate/Activate on Demand

So here is my layout, I have a 3D Menu system working and they are all in a parent/ child relationship. So for example I have my GuiRoot variable which is just an empty game object then the child of that GuiRoot is going to be my MainMenu. I then have children inside of MainMenu each child has a bunch of siblings as well so it looks kind of like this:

alt text

So when i need to transition to my next menu my camera is promted to tween to the CameraLookAt and CameraPosition. I then get the name of the Parent of the currentMoveToPosition and compare my childs name with that name which should be MainMenu( and it is) but i then need to get all of the children of MainMenu and activate them which is what i am trying with the second for each.

My Problem is that the second for each isn't firing and I do not understand why not. The Print("in grouping"); never gets sent and so neither does the if below. The Printing of the currentMOveToTarget.parent.gameObject.name and guiChild.name DOES however fire. and everything inside of gui gets turned off..... I dont understand the issue with my code.

This is all a bit complicated and I don't know if i explained it very well. So if you need more explanation I can expand.

 allGuiChildren = guiRoot.GetComponentsInChildren<Transform>();
         
         // Turn off all GUI cases except for the Main Menu
         foreach( Transform guiChild in allGuiChildren )
         {
             print(" in gui children");
             // get the parent of the current move to target in this case it is main menu
             if( guiChild.name == currentMoveToTarget.parent.gameObject.name)
             {
                 Transform[] currentGuiGrouping;
                 currentGuiGrouping = guiChild.GetComponentsInChildren<Transform>();
                 print(" in current Move to target" + currentGuiGrouping);
 
                 foreach( Transform guiGroupingChild in currentGuiGrouping)
                 {
                     print("in grouping");
                     if(guiGroupingChild.name == "GuiBase"){
                         print(guiGroupingChild.name);    
                     }
                 }
                 print(currentMoveToTarget.parent.gameObject.name + " " + guiChild.name);
                 // figure out how to set the children of mainmenu true
             }
             else
             {
                 guiChild.gameObject.SetActive(false);    
             }
         }


Thanks 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

1 Reply

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

Answer by TonyLi · Aug 16, 2013 at 02:03 PM

Try:

 GetComponentsInChildren<Transform>(true)

to find the transforms of inactive objects, too.

Also, you can iterate over a transform itself. So you can do this instead:

 // Turn off all GUI cases except for the Main Menu
 foreach( Transform guiChild in guiRoot.transform ) {...}

and

 foreach( Transform guiGroupingChild in guiChild.transform ) {...}

Makes the code simpler.

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 IMTRIGGERHAPPY9 · Aug 19, 2013 at 05:05 AM 0
Share

so what is the point of GetComponentsInChildren if you can just go through it with a foreach?

avatar image TonyLi · Aug 19, 2013 at 12:55 PM 0
Share

The foreach iterates through the transforms, but only the transforms. With GetComponentsInChildren you can get an array of any type of component (collider, renderer, etc).

avatar image odival · Dec 31, 2014 at 07:36 PM 1
Share

Sorry if this sounds dumb, but wouldnt that solution only acess the children of child components? What if I need to enable the gameobject (that is a children of a child) itself?

avatar image TonyLi · Dec 31, 2014 at 10:43 PM 0
Share

Every component has a reference to its GameObject. So you can reference guiGroupingChild.gameObject. Also, GetComponentsInChildren() is recursive, so it'll get grandchildren and so on, while foreach over the transform will only give direct children.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Switch for children not working 1 Answer

Make a simple tree 1 Answer

Instantiating as a child error 1 Answer

Children must not flip with parent. 3 Answers

Cycling Weapons, using C# int string problem 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