x


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);
       }
    }
more ▼

asked May 31 '13 at 12:39 AM

Mithosbluefish gravatar image

Mithosbluefish
20 1 5 8

Is this all located in Update?

May 31 '13 at 05:05 AM Benproductions1

Wow, herp derp :P thanks :P

May 31 '13 at 06:11 PM Mithosbluefish

I made that an answer so you can accept it and this question can be forever closed :)

Jun 01 '13 at 08:24 AM Benproductions1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Place that code in the Update function and it will work :)

more ▼

answered Jun 01 '13 at 08:24 AM

Benproductions1 gravatar image

Benproductions1
12.2k 10 13 32

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2139
x1379
x1028
x754
x627

asked: May 31 '13 at 12:39 AM

Seen: 361 times

Last Updated: Jun 01 '13 at 08:24 AM