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
0
Question by Aspect13 · Mar 16, 2019 at 05:48 PM · gameobjectgameobjectshierarchychildrenchild object

Get children of another object in hierarchy.

Hello, I want to get children objects of another object in hierarchy which does not contain script in which I'm coding, and then disable the children objects through code:

 SetActive(false);

Let's say my script is attached to an object called "GameManager" and I want to get children objects of an object called "MenuPanel" and disable them.

How do I do it? I don't see any function which allows me to do it. I can only see GetComponentsInChildren etc. but I want don't want to get components, I want to get CHILDREN OBJECTS. I tried getting component GameObject but it seems it is not a component.

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 Masterio · Mar 16, 2019 at 08:34 PM 0
Share

Try get the Transform then access to the game object trough this.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mujpir · Mar 16, 2019 at 10:36 PM

As @Masterio said , You should first find the gameObject you want to set its children to be disabled . So use GameObject.Find("MenuPanel") to find the MenuPanel object , Then use its transform to access its childs and then disable theme like this :

 var menuPanelTransform = gameObject.Find("MenuPanel").transform;
 menuPanelTransform.FindChild("child_0").gameObject.SetActive(false);

If you can , reference "MenuPanel" gameobject in your script , So you don't have to find it every time.

Comment
Add comment · Show 2 · 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 Masterio · Mar 17, 2019 at 09:47 AM 1
Share

The best solution for this as @mujpir said is using the references:

$$anonymous$$ake an handle variables for all important objects like : $$anonymous$$enuPanel 0, $$anonymous$$enuPanel 1, ... Pause$$anonymous$$enu, etc.

Example:

     public GameObject menu_0;
     public GameObject menu_1;
     public GameObject menu_2;


Then when you want to activate one of it just execute the code:

 public void Show$$anonymous$$enu(int menu_id)
 {
     switch(menu_id)
     {
     case 0 : 
     {
         menu_0.SetActive(true);
         menu_1.SetActive(false);
         menu_2.SetActive(false);
         break;
     }
     case 1 : 
     {
         menu_0.SetActive(false);
         menu_1.SetActive(true);
         menu_2.SetActive(false);
         break;
     }
     case 2 : 
     {
         menu_0.SetActive(false);
         menu_1.SetActive(false);
         menu_2.SetActive(true);
         break;
     }
     }
 }

You can use an array for game objects if you are using a lot o menus and activate wanted menu by the array index.

avatar image Aspect13 · Mar 17, 2019 at 03:26 PM 0
Share

@mujpir, @$$anonymous$$asterio I have to do it for every single child? I thought of making an array and then do foreach loop and disable them but I guess It's not possible. Is there a way to get every single child of an object? If not, will it work with RectTransform component?

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

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

How Should I Get a List of Child Objects 2 Answers

True scale of a visible GameObject with many childs 1 Answer

Hierarchy Foldout Variables Wanted 3 Answers

Assign multiple childs as gameobjects through script 1 Answer

This does not make sense this.gameObject.transform.Find() why? 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