- Home /
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?
foreach is a C# thing if memory serves, so it's right here. Also, floorDistance not being initialised is in theory correct, but lachesis should keep in $$anonymous$$d that primitive types, like float
, do not have a concept of "null" (though floats do have a concept of NaN, but this is not the same). Ins$$anonymous$$d, when you create a float and don't assign it, it will be assigned to 0. If you were using C#, you could use the nullable operator to box the float, allowing it to be assigned to null (by defining the type as float?
rather than just float
). There may be an alternative in UnityScript, but I am not as familiar with it - it may be worth looking up nullable types though.
Despite this, the only thing that could cause line 26 to throw that exception is if childBalls
is null. This means that you GetComponent call is returning no components. This may be because your object has no children, it's worth double checking that.
Answer by lachesis · Sep 18, 2013 at 12:31 AM
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:
http://answers.unity3d.com/questions/215597/why-cant-i-create-the-getcomponentsinchildrenrende.html
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613114349im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Roguelike2d example Walls fail to cast correctly in Player:OnCantMove 2 Answers
NullReferenceException After A Few Hours? 1 Answer
Object reference not set to an instace of an object (JS) 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
NullReferenceException 3 Answers