- Home /
very weird NullReferenceException error causeing one script to break another
Having an incredibly annoying isue with a new script and if anyone could shed somelight on th issue it would be much appreciated.
I have a script i'm using to allow gameobjects to orbit a larger gameobject which is acting like a planet:
var rotationSpeed = 120.0; var translationSpeed = 10.0; var height = 2.0; //height from ground level private var centre : Transform; //transform for planet private var radius : float; //calculated radius from collider var planet : SphereCollider; //collider for planet
function Start ()
{
//consider scale applied to planet transform (assuming uniform, just pick one)
radius = planet.radius * planet.transform.localScale.y;
centre = planet.transform;
//starting position at north pole
transform.position = centre.position + Vector3(0,radius+height,0);
}
function Update ()
{
//translate based on input
var inputMag = Input.GetAxis("Fire1")*translationSpeed*Time.deltaTime;
transform.position += transform.forward * inputMag;
//snap position to radius + height (could also use raycasts)
targetPosition = transform.position - centre.position;
var ratio = (radius + height) / targetPosition.magnitude;
targetPosition.Scale(Vector3(ratio, ratio, ratio) );
transform.position = targetPosition + centre.position;
//calculate planet surface normal
surfaceNormal = transform.position - centre.position;
surfaceNormal.Normalize();
//GameObject's heading
var headingDeltaAngle = Input.GetAxis("Fire2") * Time.deltaTime * rotationSpeed;
headingDelta = Quaternion.AngleAxis(headingDeltaAngle, transform.up);
//align with surface normal
transform.rotation = Quaternion.FromToRotation( transform.up, surfaceNormal) * transform.rotation;
//apply heading rotation
transform.rotation = headingDelta * transform.rotation;
}
This code is going to be accessed by two prefabs which will be spawned onto the planet. The first one is called tornadogameobject and i have(had) a working script which aquired the the planet gameobject (Sphere). I tested the script and it was working perfectly:
var sphereColl : SphereCollider;
function Start ()
{
sphereColl = GameObject.Find("Sphere").GetComponent(SphereCollider) as SphereCollider;
transform.Find("tornadogameobject").GetComponent("newgrav").planet = sphereColl;
}
I then made another version of the script attaching it to the other prefab, only changing the name found to reboundcirclegameobject:
var sphereColl : SphereCollider;
function Start ()
{
sphereColl = GameObject.Find("Sphere").GetComponent(SphereCollider) as SphereCollider;
transform.Find("reboundcirclegameobject").GetComponent("newgrav").planet = sphereColl;
}
when i tested this prefab, it came up with a NullReferenceException, which is surely strange as the other version of the script worked perfectly. On top of that when i went to retest the other prefab it had now stopped working also bringing up the NullReferenceException, very strange considering it had previously worked perfectly. I tested the tornadogameobject prefab on its own and with the reboundcirclegameobject and it made no difference.
I'm at a bit of a loss at what to do right now as the really doesn't make any sense to me as to why this would happen. Are the two scripts cancelling each other out because they both want to access the same GameObject? if anyone could shed some light on this issue i would be eternally gratefull.
Answer by Alec-Slayden · Apr 28, 2011 at 03:14 AM
This seems to be the problem:
targetPosition = transform.position - centre.position;
That's line 26, sending out the null reference exception. This is a get_position exception, and it's the first time the Update uses the centre transform.
What this means:
It would appear your newgrav Start() is running before your prefab Start(). This means that This:
centre = planet.transform;
is assigning a null value to centre, because there isn't a collider assigned to planet yet.
change the prefab scripts to Awake(), which is run first, to ensure the collider gets assigned before centre does.
Answer by DaveA · Apr 27, 2011 at 11:10 PM
Make sure you have a game object with that exact spelling (Case Is Important) in your scene. Use Debug.Log statements to verify things are getting where they should. And/or use the debugger. I usually break up the line something like:
var go = transform.Find("reboundcirclegameobject");
if (go)
var ng = go.GetComponent("newgrav");
if (ng)
ng.planet = sphereColl;
so it's easier to see which line is failing (what's not found) and you can put Debug.LogError statements in 'else' there too.
I will give that a go asap thanks very much. I am leaning towards the issue being something wrong with unity itself though. I was just giving another playtest and its applying the script but still telling me there's a null reference, when there obviously isn't as the scripts doing what its supposed to. I changed the height variable on the newgrav script to check and its affecting the prefab. The other script that previously had worked is still failing though, have you ever heard of something like this before??
@loadpixel: it's not Unity. If you have a null reference exception, you have failed to get a reference (it's therefore null), simple as that.
It must be getting a reference or it would be flying of the Sphere, as it was doing prior to its sudden change of heart. Also why would the other prefab (tornadogameobject) stop referencing, when i had previously playtested it and it worked perfectley, and have since made no changes to the script? that just doesn't make any sense to me.
it has reverted back to flying of the sphere again, but i have checked in the inspector and is assigning the reference Sphere. I took a screencapture as i thought this might be a little clearer. As you can see the reference is being assigned, but for some reason ignoreing it: http://youtu.be/FpzczGYUrVI
Your answer
Follow this Question
Related Questions
NullReferenceException from a script after linking prefab with drag and drop in inspector. WTF? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Show and Hide a prefab or GameObject 10 Answers
cloned game object wont call script right... 2 Answers
NullReferenceException problem 2 Answers