Why do I get invalid cast with this code?

var zombieLimbs:Transform;
zombieLimbs= gameObject.GetComponentsInChildren(Transform);
for(var limb :Transform in zombieLimbs)
{
//Do something here

	}

It’s practically the same code as the documentation except theat the documentation uses a hingejoint component instead. ???

The error is: invalidCastException: Cannot cast from source type to destination type.

try this instead:

zombieLimbs= gameObject.GetComponentsInChildren(Transform)as Transform;

GetComponentsInChildren returns Component. Change to this:

var zombieLimbs: Component[];
zombieLimbs = gameObject.GetComponentsInChildren(Transform);
for(var limb :Transform  in zombieLimbs){
    // do something
}

NOTE: This code returns all transforms down in the hierarchy, including this object’s transform and all its grandchildren, if any. If you want only the child transforms, the code should be like this:

for (var child in transform){
    // child in this case is Object - convert to Transform in limb:
    var limb: Transform = child;
    // do something
}