Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

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

Discussion in 'Scripting' started by Unity-Coder-2020, Oct 26, 2020.

Thread Status:
Not open for further replies.
  1. Unity-Coder-2020

    Unity-Coder-2020

    Joined:
    Apr 16, 2020
    Posts:
    3
    I keep getting this error even after completely following a tutorial from a relatively well-known source (Brackeyes). Here's my code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Movement : MonoBehaviour
    7. {
    8.  public CharacterController controller;
    9.  
    10.  public float speed = 12f;
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         float x = Input.GetAxis("Horizontal");
    16.         float z = Input.GetAxis("Vertical");
    17.  
    18.         Vector3 move = transform.right * x + transform.forward * z;
    19.  
    20.         controller.Move(move * speed * Time.deltaTime);
    21.     }
    22. }
    Any help will be appreciated!
     
  2. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    You need to assign the controller in the inspector. Drop the object with your character controller script into your controller property.
     
    addy925 and Xtyhl like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,420
    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:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

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

    PepperGaming

    Joined:
    Feb 18, 2021
    Posts:
    2
    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)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,420
    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.
     
    PepperGaming likes this.
  6. PepperGaming

    PepperGaming

    Joined:
    Feb 18, 2021
    Posts:
    2
    How do I identify what is a null
    I'm new to unity and vs
    So I don't know much
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,420
  8. ELatvija

    ELatvija

    Joined:
    Sep 16, 2021
    Posts:
    2
    Thanks this helped
     
  9. jjmccool22

    jjmccool22

    Joined:
    Jun 4, 2022
    Posts:
    1
    reread my code 3 times and googled it you helped me alot
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,700
    Thread Closed.
     
Thread Status:
Not open for further replies.