- Home /
GetComponentsInChildren() doesn't works like I expect
var allLights : Light[];
function Start () {
allLights = GetComponentsInChildren(Light) as Light[];
Debug.Log(allLights);
}
This script is attached to a gameObject (Semaphore Regulator) with the next children structure (you can also see the console output for the debug):
Answer by HarshadK · Dec 03, 2014 at 11:35 AM
You are currently using Component.GetComponentsInChildren. You need to use GameObject.GetComponentsInChildren() as:
allLights = gameObject.GetComponentsInChildren(Light) as Light[];
The main difference as in the documentation page for GameObject.GetComponentsInChildren() states that:
The search for components is carried out recursively on child objects, so it includes children of children, and so on.
Which is what you need as your lights are children of children of your Semaphore Regulator game object.
Your answer
Follow this Question
Related Questions
Get SkinnedMeshRenderer as Renderer? 1 Answer
GetComponentsInChildren Problem 1 Answer
Invalid Cast error when my format seems the same as the script reference example? 1 Answer
GetComponentsInChildren contains null elements when fetching MeshRenderers 1 Answer
Is it not possible to include "if-then" conditionals within foreach loops? 1 Answer