- Home /
Question by
Mithosbluefish · May 31, 2013 at 12:39 AM ·
movementcharactercharactercontrollercontrollernullreferenceexception
CharacterController NullReferenceException dispite attached to object
Hi,
I am writing a movement script using CharacterController. I have a characterController component attached to my object, but, it keeps returning a NullReferenceException. I have tried creating a new object as well as importing one of the example objects, but nothing seems to work. Below is all the code in the script that mentions the CharacterController.
private var character : CharacterController;
character = GetComponent(CharacterController);
if(this.transform.position != standardPos)
{
var target : Vector3 = standardPos;
target.y = transform.position.y; // keep waypoint at character's height
var moveDirection : Vector3 = target - transform.position;
if(moveDirection.magnitude < 1)
{
transform.position = target; // force character to waypoint position
}
else
{
Accelerate();
transform.LookAt(target);
character.Move((moveDirection.normalized*speed)*Time.deltaTime);
}
}
Comment
I made that an answer so you can accept it and this question can be forever closed :)
Best Answer
Answer by Benproductions1 · Jun 01, 2013 at 08:24 AM
Place that code in the Update
function and it will work :)