how do i fill an array with an objects children?

im struggling to get running code that will take all the children of an object and change a variable

example code:

var master : Transform;// has a few wheelcollider children
    
var power : float = 1.0;

private var wheelsArray : Component[];
    
function start() {
wheelsArray = master.GetComponentsInChildren(WheelCollider) ;
}
    
function Update () {
    
for(var wheel : WheelCollider in wheelsArray){
wheel.motorTorque = power ;
};
    
}

produces a NullReferenceException

cutting out the array stuff and executing

for(var wheel : WheelCollider in master){
wheel.motorTorque = power ;
};

produces MissingReferenceException: The variable master of ‘testScript’ doesn’t exist anymore.

setting the master vars type as a Component or GameObject causes slightly different errors like ApplicationException: Argument is not enumerable

what am i misunderstanding? why would the variable cease to exist?

Try setting wheelsArray to an array of WheelCollider

and call GetComponentsInChildren(WheelCollider) as WheelCollider

The computer forgets it’s a WheelCollider when it downcasts to Component. It’s thinking, ‘Wait a minute, there’s no WheelColliders in this array, only Components!’

Alternatively I think you could cast it back up to a WheelCollider before using it in the foreach loop.

Hope that helps.