How do I loop through children and then their children until there are no more children?

I want to be as neat as possible so currently I am passing a transform to the function as follows..

function CalculateSteps(transformToCheck:Transform)
{

    var child:Transform;

    for(var child:Transform in transformToCheck)
    {

        var childsToy:Toy;
        childsToy=child.gameObject.GetComponent(Toy);

        if(childsToy==theCorrectToy)
        {
            print("This is the one. you can stop looking.");
            return;
        }
    }

    //if it gets to here and it still hasnt found anything do it again using the child as the transform
    scannedThroughSetOfChildren+=1;
    if(child)
    {CalculateSteps (child);}//do this function again but with a child transform argument....

}

scannedThroughSetOfChildren only ever maxes at 2 scans..I want it to scan infinitely through all the parent's children, grand children, great children etc...until 'theCorrectToy' is found..

Basically i want to find out how many 'generations' ago a child had a specific toy.

Searching is a fairly well-documented topic in computer science in general. You should try searching (answers, google, forums, etc.) before posting questions.

You aren't clear if you want to find the most recent generation to have the toy or if it even mattered.

Here are two simple approaches (both with breadth-first and depth-first searches):

I return the owner of the toy and its generation. You can change the return statements as you like if you only wanted the generation.

Recursive

Note that there may be a limit on the number of recursions allowed which may be why you are experiencing the problem that you are.

DFS

function findToy(parent : Transform, target : Toy) : Array
{
    return findChildToy(parent, target, 0);
}

private function findChildToy(parent : Transform, target : Toy, generation : int) : Array
{
    if(parent.GetComponent(Toy) == target) return new Array(parent, generation);
    generation++;
    for(var child : Transform in parent)
    {
        var result = findToy(child, target, generation);
        if(result != null) return result;
    }
    return;
}

BFS

function findToy(parent : Transform, target : Toy) : Array
{
    var queue : Array = new Array();
    queue.push(parent);
    return findChildToy(queue, target, 0);
}

private function findChildToy(queue : Array, target : Toy, generation : int) : Array
{
    var parents : int = queue.length;
    for(var i : int = 0; i < parents; i++)
    {
        var parent : Transform = queue.shift();
        if(parent.GetComponent(Toy) == target) return new Array(parent, generation);
        for(var child : Transform in parent) queue.push(child);
    }
    if (queue.length == 0) return;
    return findChildToy(queue, target, generation++);
}

Non-recursive

DFS

function findToy(parent : Transform, target : Toy) : Array
{
    var stack : Array = new Array();
    stack.push(new Array(parent, 0));
    while (stack.length != 0)
    {
        var parent : Array = stack.pop();
        if(parent[0].GetComponent(Toy) == target)
            return parent;
        parent[1]++;
        for(var child : Transform in parent[0])
            stack.push(new Array(child, parent[1]));
    }
}

BFS

function findToy(parent : Transform, target : Toy) : Array
{
    var queue : Array = new Array();
    queue.push(new Array(parent, 0));
    while (queue.length != 0)
    {
        var parent : Array = queue.shift();
        if(parent[0].GetComponent(Toy) == target)
            return parent;
        parent[1]++;
        for(var child : Transform in parent[0])
            queue.push(new Array(child, parent[1]));
    }
}

you can use "GetComponentsInChildren()" instead of GetComponent();

static function FindTransform(parent : Transform, name : String) : Transform
{
    if (parent.name == name) return parent;
    var transforms = parent.GetComponentsInChildren(Transform);
    for (var t : Transform in transforms)
    {
        if (t.name == name) return t;
    }
    return null;
}