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
0
Question by bpears · Oct 17, 2013 at 11:24 AM · setactivecomponentsenabled

Does .SetActive work with components?

Wondering if I can use .SetActive to iterate through a GameObject and it's children to enable/disable that part of all of them.

Otherwise, I'll stick to GetComponentsInChildren.

If not, this would be a damn cool feature.

Comment
Add comment · Show 3
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 ShadoX · Oct 17, 2013 at 11:43 AM 0
Share

Isn't that exactly what you do with GetComponentsInChildren ? .SetActive is just setting a bool value, so I don't see how that would be able to iterate over all components of a gameObject.

avatar image bpears · Oct 17, 2013 at 11:51 AM 1
Share

Because .SetActive, when used like GameObject.SetActive = true, it becomes true for all children. However components are a little different in that way, but the only thing they would have to consider in program$$anonymous$$g it to work with components, is to check if each child has that component. I just think it would be a nice shortcut, if it isnt one already, for child components.

avatar image whydoidoit · Oct 17, 2013 at 12:12 PM 0
Share

It doesn't exist I'm afraid - and yep it would be handy :) The other thing that would be handy was if Colliders shared the same enabled status as Behaviours - which they do not :(

1 Reply

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

Answer by whydoidoit · Oct 17, 2013 at 12:19 PM

This might help:

 using UnityEngine;
 
 public static class EnableHelper
 {
       public static void EnableChildren(this Component component, System.Func<Component, bool> predicate, bool enabled = true)
       {
             foreach(var c in component.GetComponentsInChildren<Collider>(true))
                 if(predicate(c))
                  c.enabled = enabled;
             foreach(var c in component.GetComponentsInChildren<Rigidbody>(true))
                 if(predicate(c))
                  c.isKinematic = !enabled;
             foreach(var c in component.GetComponentsInChildren<Behaviour>(true))
                 if(predicate(c))
                   c.enabled = enabled;
       }
 
       public static void EnableChildren(this Component component, bool enabled = true)
       {
            component.EnableChildren((a)=>true, enabled);
       }
 
       public static void EnableChildren(this GameObject go, bool enabled = true)
       {      
          go.transform.EnableChildren((a)=>true, enabled);
       }
 
       public static void EnableChildren(this GameObject go, System.Func<Component, bool> predicate, bool enabled = true)
       {
           go.transform.EnableChildren(predicate, enabled);
       }
 
 }

Then you can just do:

     gameObject.EnableChildren();

     gameObject.EnableChildren(false);

     gameObject.EnableChildren(c=>c.CompareTag("Something"));
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 whydoidoit · Oct 17, 2013 at 12:20 PM 0
Share

It's more like SetActiveRecursively than the new SetActive though.

avatar image bpears · Oct 17, 2013 at 12:40 PM 0
Share

oooo me likey! Thanks again!

avatar image whydoidoit · Oct 17, 2013 at 12:43 PM 0
Share

Oops fixed a typo!

avatar image bpears · Oct 17, 2013 at 12:50 PM 0
Share

this needs to be integrated officially :)

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

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

How to set false components to true? 1 Answer

sync renderer.enabled or SetActive() over network? 2 Answers

If i disable a GameObject does all it's components get disabled also? 2 Answers

Set Objects Child to Active/Inactive(Solved) 5 Answers

Why are some components not inherited from Behaviour when they should? 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