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 Pramod659 · Jun 25, 2017 at 04:06 PM · gameobjecthierarchyaccess

How to hide every game object as per Hierarchy in active scene?

I have a structure as shown in the image attached.

My requirement is to hide the game objects one by one in the order given below

Child1 >> Child2 >> Sub assembly1 >> Sub root1 >> Child 3 >> Child4 >> Sub root 2.

The whole assembly is very big and has lot of such sub assemblies and hence I need to script this. I tried using Transform.GetSiblingIndex and gameObject.GetComponentsInChildren.() in loop without any success. Since I am new to C# scripting. Any help in this regard is highly appreciable.

unbenannt.png (3.7 kB)
Comment
Add comment · Show 5
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 pako · Jun 25, 2017 at 06:34 PM 0
Share

@Pramod659 your question is not very clear.

What do you mean by "hide"? and what user action would trigger the "hiding"?

Also, is this something you want to do in the editor only, or what?

avatar image Pramod659 · Jun 26, 2017 at 02:29 PM 0
Share

@pako By "hide" I mean setting "render.enabled" to "false" for each gameobject but as per the order stated above. Initially, just one user action, say user hits "H" button and the whole assembly of gameobjects should disappear in the order as stated above.

avatar image pako · Jun 26, 2017 at 02:57 PM 0
Share

@Pramod659 I understand that order of disabling is important, but it seems to me that unless there's some kind of delay between each renderer disabled, the user will not be able to notice the order, since they will all be disabled almost instantaneously. So, if you do think that a delay would be better to show the effect, what kind of delay would be good, 0.5s, 1s?

Also, since the sub assemblies are very big, I guess dragging and dropping the renderers in a public array or List in the Inspector is out of the question? However, if it's something you'd consider (done only once), then setting the order of disabling is just a matter of adding the renderers in the correct order in the array/List.

avatar image Pramod659 · Jun 26, 2017 at 03:09 PM 0
Share

@pako Ya infact I need a delay of 1sec. It is not just one time activity for one assembly. I need it for different levels and corresponding assemblies of my project.

Dragging and dropping the renderers in a public array or List in the Inspector is out of the question? ---YES

avatar image pako · Jun 26, 2017 at 04:28 PM 0
Share

@Pramod659 you will need to set the order somehow, it cannot be done automatically, since the order is not entirely sequential.

From what you say, it seems that hard coding any kind of array to set the order would be too laborious to implement. So, the best bet is to figure out an algorithm to do it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by pako · Jun 26, 2017 at 04:28 PM

From the example you posted the following seems like a solution:

  1. Create a collection of all roots (they don't have a parent) - I think it's best to tag each root and use the FindGameObjectsWithTag command to create the collection. There are other ways, and it's up to you to decide, as you have the whole picture. However, FindGameObjectsWithTag returns a collection of GameObjects, but you need an array of transforms. So, you need to iterate through the returned GameObject collection and access the transforms to add to your Roots collection.

  2. Create a collection with all children of a root (indeces 0, 1, 2, ... n). These are the subroots. You can iterate through the collection of roots, and for each root, add all its children to a new subroot collection. This will result in a subroot collection for each root. The GetChild(int index) command can be used for this: https://docs.unity3d.com/ScriptReference/Transform.GetChild.html

  3. Each subroot has either a subassembly as a child, and several grandchildren (the children of the subassembly (e.g. child 1, child 2) OR children with no children (e.g. no subassembly, child 3, child 4). You can iterate through each subroot and check if its first child (index 0) has any children, by using transform.childCount. If the child count is greater than 0 then it's a subassembly, otherwise it's directly a child with no other children: https://docs.unity3d.com/ScriptReference/Transform-childCount.html

  4. While on #3, create a collection of subassemblies, and a collection of the children of each subassembly, and also a collection of the children directly under a subroot.

  5. The process of creating the collections in #1 - #4 above is naturally in the general order that you want to disable the renderers.

  6. It might be best to create the collections once and save them, or you might prefer to create the collections every time the game is started. As you are iterating through the collections, the logic for disabling the renderers is the following:

  7. When you find a subassembly, iterate through its children and disable their renderers, then disable the renderer of the subassembly, and then disable the renderer of the subroot.

  8. When you find children directly under a subroot, iterate through the subroot's children and disable their renderers, and then disable the renderer of the subroot.

I hope this helps you.

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 Pramod659 · Jun 27, 2017 at 02:14 PM 0
Share

@pako Thanks for your valuable suggestion. I shall try and assemble pieces of code to make the logic work which you have stated above and hopefully get it working

avatar image pako · Jun 27, 2017 at 02:52 PM 0
Share

@Pramod659 let me know if you need any clarifications.

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

88 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

Related Questions

Accessing a GameObject from an EditorWindow 3 Answers

Access a instantiated gameobject from a enemy spawner 4 Answers

SOLVED - Accessing a C# script value from another GameObject's JavaScript 2 Answers

Using Empty GameObjects to organize hierarchy? 2 Answers

Best workflow for DontDestroyOnLoad method regarding Unity Editor? 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