- Home /
Rotation scripts logging error messages?
Ok, so I've been working on a little space game and I have a couple buggy scripts. My ship is flying around and rotating fine. It's model along with some thruster particles, lights and such are all attached to an empty GameObject. The ship rotates around fine, but the thrusters didn't want to cooperate with the controller script so I made a couple other ones to sync the ship's rotation with theirs. Here's the script attached to the ship model(and yes, I know my code is sloppy. I'm working on it x3):
using UnityEngine;
using System.Collections;
public class StartershipRotation : MonoBehaviour {
public float rotationX;
public float rotationY;
public float rotationZed;
void Start()
{
rotationX = 0;
rotationY = 0;
rotationZed = 0;
}
void Update()
{
rotationX = gameObject.transform.rotation.eulerAngles.x;
rotationY = gameObject.transform.rotation.eulerAngles.y;
rotationZed = gameObject.transform.rotation.eulerAngles.z;
}
}
That script should log the ship's rotation values to some variables. This next script attached to the thrusters reads it:
using UnityEngine;
using System.Collections;
public class AutoRotate : MonoBehaviour {
public float selfX;
public float selfY;
public float selfZ;
void Start()
{
transform.Find("Startership");
}
void Update()
{
//Update Variables
selfX = transform.Find("Startership").GetComponent<StartershipRotation>().rotationX;
selfY = transform.Find("Startership").GetComponent<StartershipRotation>().rotationY;
selfZ = transform.Find("Startership").GetComponent<StartershipRotation>().rotationZed;
//Change Self Rotation Values
transform.Rotate(selfX, selfY, selfZ * Time.deltaTime);
}
}
The error I get is: "NullReferenceException: Object reference not set to an instance of an object AutoRotate.Update () (at Assets/Scripts/AutoRotate.cs:21)"
Any help would be greatly appreciated.
Thanks in advance, Jaden14541
Answer by gjf · Jun 27, 2014 at 10:11 AM
that error is likely caused by one or both of the following:
1) there's no object named 'Startership'.
2) there's no StartershipRotation
script attached to the 'Startership' object.
Start()
in StartershipRotation
is unnecessary.
also, what is the point of transform.Find("Startership");
in Start()
of AutoRotate
?
that function returns the Transform
component of a child that you're not doing anything with.
you could probably achieve the desired results in a simpler way (but without seeing all of the code, it's hard to advise).
the best advice is to find a way to get the thrusters working with the controller. post that script, and ask for help fixing it...