- Home /
The question is answered, right answer was accepted
GetComponents vs GetComponentsInChildren
Hello guys !
I just encountered a little problem with these two functions, so I went to the online doc and I saw a really important difference : GetComponents seems to work on inactive game objects, contrary to GetComponentsInChildren ...
Is there an easy way to get the components in children of an inactive game object ?
Simply traverse the children and use GetComponents() on each one.
If you want to get all the rigid bodies for example:
     ArrayList components = new ArrayList();
     foreach (Transform child in transform) {
         components.AddRange(child.gameObject.GetComponents<Rigidbody>());
     }
How do you traverse the children in an inactive gameobject ?
An easy way could be to activate it, find my components, and deactivate it, but I think it is very ugly ...
Did you test my code? I think it traverses all active or inactive gameobjects. Otherwise how would you find the inactive children in order to activate them?
Actually I'm looking for audiosources in a gameobject children to impact their volume. But I don't want to activate anything.
The code I will make can be called a lot in a really short time (it will be plugged to a slider), so I need it to be as quick as possible.
I'll try your code to test if this works.
Answer by Bunny83 · May 23, 2013 at 09:38 AM
Depending on the Unity version you're using you can simply pass an additional parameter which will include inactive objects as well:
 GetComponentsInChildren<Renderer>(true)
Oh my... I didn't even see the optional parameter ... thanks
Follow this Question
Related Questions
C# Specifying which Children to add to the Array 1 Answer
foreach script check if boolean is true 3 Answers
An array for Particle Emitters? 2 Answers
Gameobject with many child objects disable mesh not working. 0 Answers
GetComponents array in C# error ? 4 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                