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.
@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.
– Eric5h5