- Home /
Simple Character movement giving object error
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.
CameraController.Update () (at Assets/Game/Scripts/Camera/CameraController.js:204) function Update () { var controller : CharacterController = GetComponent(CharacterController);NullReferenceException: Object reference not set to an instance of an object
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);
We can´t know what´s your problem because we don´t know which line is the 204th. Edit your question with this info. An a way to find out which of the variables is null is to do the following, for example, if you haev the line
var1.var2.var3.DoSomething;
do this just above the line that throws the error
print("var1 = "+var1); print("var2 = "+var2); print("var3 = "+var3);
then you can see on your console windows exactly which of the terms is null
Answer by aldonaletto · Sep 26, 2012 at 11:56 PM
That's a very weird code! You must use character.Move (preferably) or transform.Translate, not both at the same time. And you don't need to check the keys WASD individually: Input.GetAxis does it at once for you. Your code could be as simple as this:
function Update () { var controller : CharacterController = GetComponent(CharacterController);
moveDirection = Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
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();
}
}
}
Anyway, I could not find anything in the code you've posted that could cause that error. Which one is line 204?
Aldo, unfortunately.. without get key its not picking up the key being pressed once so ever. I should add... the code WOR$$anonymous$$S.. it just produces that error on controller.$$anonymous$$ove whenever the key is pressed. And without transform.Traslate... it doesn't work at all.
Well, this should work, but I suppose that this error is preventing your script to work normally. If the error points to controller.$$anonymous$$ove, then probably the CharacterController isn't being found: you've forgot to add a CharacterController to your character, or it's added to an object and your script is attached to another one, possibly a child or parent - notice that the script and the CharacterController must be attached to the same object! Another possibility: the script was inadvertently attached to more than one object (again, probably a character child).
If you can't see any of these errors, add the following line to your script:
function Start(){
if (!GetComponent(CharacterController)){
print("CC not found - script attached to "+name);
}
}
Check if that damned button Collapse isn't active in the Console view, and click run: if the CharacterController isn't being found, the name of the object to which the script is attached will appear in the message, thus you can more easily know what's going wrong.
Your answer
