Recursing through a bone hierarchy: Stack Overflow

My code is at the bottom of the post. The second function, FindChildByName is most important.

It takes a transform (a root bone of a skeleton) and a target name to search for. The idea is that it recurses through the hierarchy until it finds a bone with a matching name, or returns null.

As soon as i run it (without the print statement), unity freezes for approximately two seconds and then halts the script with a StackOverflowException

If i try to stick a debug message in there as shown below, it just freezes indefinitely, because print is very slow and its getting stuck in an infinite loop.

So i’m struggling to debug it. Where am i going wrong?

void ProcessMeldPair(GameObject root, SkinnedMeshRenderer sub)
        //creating a new object? i guess we're cloning the sub
        GameObject newObj = new GameObject(;
        newObj.transform.parent = root.transform;

        //Cloning renderer
        SkinnedMeshRenderer newRenderer = newObj.AddComponent<SkinnedMeshRenderer>();

        //Assemble bone structure?
        Transform[] subBones = new Transform[sub.bones.Length];
        int i;
        for (i = 0; i < sub.bones.Length;i++)
            subBones *= FindChildByName( sub.bones[ i ].name, root.transform );*


//Assemble Renderer
newRenderer.bones = subBones;
newRenderer.sharedMesh = sub.sharedMesh;
newRenderer.sharedMaterials = sub.sharedMaterials;

private Transform FindChildByName( string targetName, Transform bone )
print("Finding bone: " + targetName);
Transform returnVar = null;
if ( == targetName)
return bone;

foreach (Transform child in bone)
returnVar = FindChildByName(targetName, bone);
if (returnVar)
return returnVar;
return null;

Line 35 should be

returnVar = FindChildByName(targetName, child);

instead of

returnVar = FindChildByName(targetName, bone);