Loop through the bone hierarchy?

I’m trying to loop through my character’s bone hierarchy and make all of it’s rigid bodies kinematic.
I tried this recursive function, but it crashed the unity.

function TraverseHierarchy(root: Transform)
 {
  for (var child : Transform in transform) 
  {
    child.rigidbody.isKinematic=true;
    TraverseHierarchy(child);
  }
}

Has anyone tried something similar?
Thanks

i believe setActiveRecursively sets all elements in the object to true or false. you should look into that

setActiveRecursively function recursively loops through the child objects and activate/deactivate them. What I want is change their isKinematic state.

You have an infinite recursion in your code, caused by “in transform”.
It should be something like this:

function TraverseHierarchy(parent: Transform)
{
  for (var child : Transform in parent) 
  {
    child.rigidbody.isKinematic=true;
    TraverseHierarchy(child);
  }
}


...
TraverseHierarchy(transform);
...