- Home /
UnassignedReferenceException when its already assigned
hey
ive written a script and it says that one of my variables has not been asigned??? very confused because its assigned and the script still works fine its just this error keeps coming up. sing UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
// Use this for initialization
CharacterController charController;
Animator Anim;
public GameObject camera;
public float Sensitivity = 5;
public float CameraSmooth = 0.1f;
[HideInInspector]
public float xRotationCam;
[HideInInspector]
public float yRotationCam;
float camXrotation;
float camYrotation;
void Start () {
Anim = GetComponent<Animator>();
charController = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
v = Input.GetAxis("Vertical"); // gets your mouse inputs
h = Input.GetAxis("Horizontal");
//****CAMERA MOVEMENT****\\
yRotationCam -= Input.GetAxis("Mouse Y") * Sensitivity;
xRotationCam += Input.GetAxis("Mouse X") * Sensitivity;
yRotationCam = Mathf.Clamp(yRotationCam, -90, 90);
camYrotation = Mathf.Lerp(camYrotation, yRotationCam, CameraSmooth);
camXrotation = Mathf.Lerp(camXrotation, xRotationCam, CameraSmooth);
camera.transform.localRotation = Quaternion.Euler(camYrotation, 0, 0);//error on this line
transform.rotation = Quaternion.Euler(0, camXrotation, 0);
}
}
ive shortened my script to make it easier to read (Hopefully)
UnassignedReferenceException: The variable camera of 'PlayerController' has not been assigned. You probably need to assign the camera variable of the PlayerController script in the inspector. PlayerController.Update () (at Assets/_A Player/Scripts/PlayerController.cs:129)
thanks ~Scott
Would you have the script somewhere else?
Add a print to check:
void Start(){
print(this.name + " " + GetInstanceID());
}
If you get it twice with different ID you have twice, if not, at least we removed one reason.
Answer by WeirderChimp · Apr 14, 2014 at 09:32 AM
hey i only have one of these scripts in my project. ive fixed it ive just made a new scripts and only put the camera movement stuff in it thanks anyway ;)