Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

NullReferenceException

Discussion in 'Scripting' started by tomvebrcz, Jan 15, 2021.

  1. tomvebrcz

    tomvebrcz

    Joined:
    Jan 13, 2021
    Posts:
    3
    Hello Guys,

    Can you please help me and explain what I am doing wrong. I am trying to change movement animation upon key press as per following:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public abstract class Character : MonoBehaviour
    6. {
    7.  
    8.     [SerializeField]
    9.     private float speed;
    10.  
    11.     private Animator animator;
    12.  
    13.     // We need to use protected so we do not change somewhere else by accident.
    14.     protected Vector2 direction;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         //Connect our variable animator with our Animator controller.
    20.         animator = GetComponent<Animator>();
    21.     }
    22.  
    23.     /// <summary>
    24.     /// Update is called once per frame.
    25.     /// Virtual means that Character.Update() can be overridden in sub-class.
    26.     /// </summary>
    27.     protected virtual void Update()
    28.     {
    29.         Move();
    30.     }
    31.  
    32.     /// <summary>
    33.     /// Move the player.
    34.     /// </summary>
    35.     public void Move()
    36.     {
    37.         //Make sure that the player moves.
    38.         transform.Translate(direction * Time.deltaTime * speed);
    39.  
    40.         //Once we move, animate our movement.
    41.         AnimateMovement(direction);
    42.     }
    43.  
    44.     /// <summary>
    45.     /// Animation transition upon key entry.
    46.     /// To do this we need to access Animator which contains transition parameters.
    47.     /// </summary>
    48.     /// <param name="direction"></param>
    49.     public void AnimateMovement(Vector2 direction)
    50.     {
    51.          animator.SetFloat("x", direction.x);
    52.          animator.SetFloat("y", direction.y);
    53.     }
    54. }
    55.  
    And player:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player : Character
    6. {
    7.  
    8.     // Start is called before the first frame update.
    9.     void Start()
    10.     {
    11.  
    12.     }
    13.  
    14.     /// <summary>
    15.     /// Update is called once per frame.
    16.     /// We override the Update() in Character class.
    17.     /// </summary>
    18.     protected override void Update()
    19.     {
    20.         InputForMovement();
    21.         //Access and Run Character.Update() with base.
    22.         base.Update();
    23.     }
    24.  
    25.     /// <summary>
    26.     /// Listen the player's input.
    27.     /// </summary>
    28.     private void InputForMovement(){
    29.  
    30.         direction = Vector2.zero;
    31.  
    32.         //GetKey on hold
    33.         //GetDownKey on press
    34.         if (Input.GetKey(KeyCode.W)){
    35.             direction += Vector2.up;
    36.         }
    37.         if(Input.GetKey(KeyCode.A)){
    38.             direction += Vector2.left;
    39.         }
    40.         if(Input.GetKey(KeyCode.S)){
    41.             direction += Vector2.down;
    42.         }
    43.         if(Input.GetKey(KeyCode.D)){
    44.             direction += Vector2.right;
    45.         }
    46.     }
    47. }
    48.  
    But I am getting the following error:

    Code (CSharp):
    1. NullReferenceException: Object reference not set to an instance of an object
    2. Character.AnimateMovement (UnityEngine.Vector2 direction) (at Assets/Scripts/Character.cs:53)
    3. Character.Move () (at Assets/Scripts/Character.cs:41)
    4. Character.Update () (at Assets/Scripts/Character.cs:29)
    5. Player.Update () (at Assets/Scripts/Player.cs:22)
    6.  
    If I am correct both direction and controller should be initialized already. What I am doing wrong?

    Thanks for any help.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Do you have an Animator component on the same GameObject as your Player component?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    The answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    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. tomvebrcz

    tomvebrcz

    Joined:
    Jan 13, 2021
    Posts:
    3
    Yes, I have. But it looks like it doesn't initialize upon Start().
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Find out why. Is this script on more than one GameObject? Print the name of the GameObject in Start()

    Look at the name that comes out. Does it have an Animator?

    Etc.etc There's always more steps to check. You will find it.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Oh I see your problem. Inheritance is generally a bad place to start learning Unity.

    Your derived class does not call base.Start()

    Get the basics working before reaching for inheritance, then refactor.

    Unity is actually a component architecture so you generally want to stick with components that do things, rather than things-that-are-things. You know, the old "When in Rome..." :)
     
    PraetorBlue likes this.
  7. tomvebrcz

    tomvebrcz

    Joined:
    Jan 13, 2021
    Posts:
    3
    Thanks, see it now and fixed it and working now. Thanks for help.
     
    Kurt-Dekker likes this.