Null reference exception with elusive cause

I’m attempting to run a UnityScript script with which I see no apparent problems. However, when I attempt to run the scene in which the script below is embedded, it throws a NullReference exception on Line 26.

#pragma strict

// Inspector variables

var triBall : GameObject;
var detector : GameObject;
var floorDistance : float;
var incrementCount : int = 10;
var incrementSize : float;

// Private variables

private var lowestActive : Vector3;

function Update () {
	if(Input.GetKeyDown("space")){
		
		// Instantiate a tri-ball and store its childrens' Transform components in an array
		
		var activeTriBall : GameObject = Instantiate(triBall, transform.position, Quaternion.identity);
		
		var childBalls : Transform[] = activeTriBall.GetComponentsInChildren(Transform);
		
		// Find the position, in Vector3, of the child ball with the least y position and store that in lowestActive
		
		for (var pos : Transform in childBalls){
			if(floorDistance == null || pos.position.y < floorDistance){
				floorDistance = pos.position.y;
			}
		}

// Calculations used to prepare and pass a variable to another script. Probably irrelevant to the question but included in case there's some connection that I can't see.

		incrementSize = floorDistance / incrementCount;
		activeTriBall.GetComponent(TriBallControl).stepRate = incrementSize;
		
		var activeDetector : GameObject = Instantiate(detector, transform.position - Vector3(0,.5,0), Quaternion.identity);
		activeDetector.transform.parent = activeTriBall.transform;
	}
}

Some context:

  • triBall is a prefab with three spherical children
  • detector is a duplicate of the triBall with renderers removed (but colliders active and set to non-kinematic) that rides in front of the triBall’s path to detect collisions early.

The script’s purpose is straightforward: I embed a keypress listener in an Update() function within a script attached to an empty GameObject titled SpawnPoint. When the player presses space, the script instantiates a prefab named “tri-ball” at the SpawnPoint’s position and stores the tri-ball in variable activeTriBall.

Once the tri-ball has been instantiated and a reference established, I declare a Transform array–childBalls–and store the Transform components of each of the three spherical child objects. I then run a for loop on each of the Transform components stored in the childBalls array to determine the lowest position.y in the array.

I use this position.y (floorDistance) to run a calculation for another script in my scene. The code likely isn’t relevant to my problem, but I’ve included it here for clarity.

The debug process identifies Line 26 as the source of the NullReference exception:

for(var pos : Transform in childBalls) 

However, given that I’ve already instantiated childBalls, I don’t see why this is so. Does anyone know why?

Solved! I was using the outdated (hopefully deprecated) version of GetComponentsInChildren.

The vanilla GetComponentsInChildren() returns an array of Component types. I was attempting to use this, then cast the array as a different type within the same statement. It appears that this is impossible:

http://forum.unity3d.com/threads/27026-Help-with-GetComponentsInChildren

An alternative would’ve been to store the output from GetComponentsInChildren(), then cast each of its separate members as a Transform within a for loop:

Instead, I used the generic version of GetComponentsInChildren(), which returns an array of the specified component type:

http://docs.unity3d.com/Documentation/Manual/GenericFunctions.html