NullReferenceException

I’m getting

NullReferenceException: Object reference not set to an instance of an object InputManager.FixedUpdate () (at Assets/Scripts/InputManager.cs46)

and

NullReferenceException: Object reference not set to an instance of an object InputManager.LateUpdate () (at Assets/Scripts/InputManager.cs50)

Here’s my code

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

public class InputManager : MonoBehaviour
{
public PlayerInput playerInput;
public PlayerInput.OnFootActions onFoot;

private PlayerMotor motor;
private PlayerLook look;
   // Start is called before the first frame update
      public static InputManager Instance { get; set; }
   
      public InputManager inputManager;
   
   private void Awake()
   {
      if (Instance != null && Instance != this)
   {

   }
   else
   {
       Instance = this;
   }
 
       playerInput = new PlayerInput();
       onFoot = playerInput.OnFoot;
       
       motor = GetComponent<PlayerMotor>();
       look = GetComponent<PlayerLook>();
       
       onFoot.Jump.performed += ctx => motor.Jump();
       
       onFoot.Crouch.performed += ctx => motor.Crouch();
       onFoot.Sprint.performed += ctx => motor.Sprint();
   }
   

   // Update is called once per frame
    void FixedUpdate()
   {
       //tell the playermotor to move using the value from our movement action.
        motor.ProcessMove(InputManager.Instance.inputManager.onFoot.Movement.ReadValue<Vector2>());
   }
   private void LateUpdate()
   {
       look.ProcessLook(InputManager.Instance.inputManager.onFoot.Look.ReadValue<Vector2>());
   }
   private void OnEnable()
   {
       onFoot.Enable();
   }
   private void OnDisable()
   {
       onFoot.Disable();
   }
}

I understand what a NullReferenceException is but I can’t figure out what is null or how to fix it

Your Singleton looks very strange, especially the gap in line 22. Moreover, there’s no point in the public InputManager inputManager field (in line 16) since you already have a reference through the static property (in line 14).

Your public field on line 16 doesn’t make sense, and it’s not assigned a value anywhere in the provided code, but you reference it in the lines below.

In the following lines:

motor.ProcessMove(InputManager.Instance.inputManager.onFoot.Movement.ReadValue look.ProcessLook(InputManager.Instance.inputManager.onFoot.Look.ReadValue

it’s completely unclear why, inside the InputManager class, you don’t access it directly but instead take a convoluted route. In fact, here it would suffice to refer to itself using “this.”

1 Like

Please don’t start multiple threads for a null reference.

You don’t even need to post for a null reference in the first place because…

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that