UnassignedReferenceException: The variable controller of Movement has not been assigned.

I keep getting this error even after completely following a tutorial from a relatively well-known source (Brackeyes). Here’s my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
 public CharacterController controller;

 public float speed = 12f;

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);
    }
}

Any help will be appreciated!

You need to assign the controller in the inspector. Drop the object with your character controller script into your controller property.

2 Likes

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

I have the same problem
I did exact same thing you said but it didn’t work.
Can you see the code(It’s the exact same)

No, you have failed to do the right thing: if you had done the right thing the error would go away.

Again, the steps are:

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.
1 Like

How do I identify what is a null
I’m new to unity and vs
So I don’t know much

http://plbm.com/?p=221

Thanks this helped

reread my code 3 times and googled it you helped me alot

Thread Closed.