Am I Mad? (GetComponentInChildren)

Lately, I’ve been coding just fine with no hiccups, minus a few bugs with the Unity Engine which I have reported. However, this one is strange.

#pragma strict

...class stuff...

    function ApplyToAll (edit : GameObject)
    {
	var childrenTrans : Transform [] = edit.GetComponentsInChildren(Transform) as Transform [];
	
	if (childrenTrans != null)
	{
	    var skipFirst : int = 0;
	    for (var child : Transform in childrenTrans)
	    {
		Apply(child.gameObject, false);
	    }
	}
	else
	{
	    Debug.Log(edit.name + " has no children!");
	}
    }

The above has been printing out “GameObject has no children”.

However, when I remove pragma strict and change the line to:

	var childrenTrans = edit.GetComponentsInChildren(Transform);

the above code works just fine.

Any ideas?

Got a screenshot of your GameObject hierarchy?

You cannot cast an object[ ] as a Transform[ ].

It’s fast to type check a single object to see if it can be cast as such, but when casting an entire array I don’t believe it can check each individual object to see if it is castable to a type transform as, depending on the size of the array that could take a very long time.

You could try

var childrenTrans : Transform[ ] = edit.GetComponentsInChildren.();

I believe that is how JS uses Generics.