Everything I’ve read says I’m doing this properly… so I have no idea what I’m doing wrong.
Its a simple move script. Press W and the camera moves forward. And I’m getting the following error.
NullReferenceException: Object reference not set to an instance of an object
CameraController.Update () (at Assets/Game/Scripts/Camera/CameraController.js:204)
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
if(Input.GetKey("w")) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(moveDirection);
moveDirection *= speed;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
if(Input.GetKey("s")) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(moveDirection);
moveDirection *= speed;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
if(Input.GetKey("a")) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(moveDirection);
moveDirection *= speed;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
if(Input.GetKey("d")) {
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.Translate(moveDirection);
moveDirection *= speed;
// Move the controller
controller.Move(moveDirection * Time.deltaTime);
}
if (Input.GetButtonDown("Fire2"))
{
mouseButton2DownPoint = Input.mousePosition;
mouseRightDrag = true;
}
if (Input.GetButtonUp("Fire2"))
{
mouseRightDrag = false;
mouseButton2UpPoint = Input.mousePosition;
}
if (Input.GetButtonDown("Fire3"))
{
print("Middle Mouse");
mouseButton3DownPoint = Input.mousePosition;
mouseMiddleDrag = true;
}
if (Input.GetButtonUp("Fire3"))
{
mouseMiddleDrag = false;
mouseButton3UpPoint = Input.mousePosition;
if (mouseButton3DownPoint == mouseButton3UpPoint) {
ClearSelectedUnitsList();
}
}
}
I was gonna remove all the charactercontroller code and instead use transform.position.
But doing that gets me errors about not being able to convert vector 3 into a float.
I’m confused here.
My apologies… the error is coming from "Controller.Move(moveDirection * Time.deltaTime);