NullReferenceException while executing 'performed' callbacks

Hello. I’m having this error with the new input system. I can’t figure out what my problem is.
Here is my error:

NullReferenceException while executing 'performed' callbacks of 'Player/Shoot[/Mouse/leftButton]'
UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)

Here is my code:

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

public class Collision : MonoBehaviour
{

    private MapRenderer mapRenderer;

    private Controls controls = null;

    private Controls Controls
    {
        get
        {
            if (controls != null) { return controls; }
            return controls = new Controls();
        }
    }

    // Start is called before the first frame update
    void Awake()
    {
        mapRenderer = GetComponent<MapRenderer>();
    }

    private Vector2 mousePos;

    private void SetMousePos(Vector2 mousePos)
    {
        this.mousePos = mousePos;
    }

    private void RightClick()
    {
        if (mousePos == null) return;

        Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y));
        Vector3 savePosition = position;
        // Perform RightClick()
    }

    private void LeftClick()
    {
        if (mousePos == null) return;

        Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y));
        Vector3 savePosition = position;
        // Perform LeftClick(
     }

    private void OnEnable()
    {
        Controls.Enable();
        Controls.Player.Look.performed += ctx => SetMousePos(ctx.ReadValue<Vector2>());
        Controls.Player.Shoot.performed += _ => RightClick();
        Controls.Player.Shoot.performed += _ => LeftClick();
    }

    private void OnDisable()
    {
        Controls.Disable();
    }

}

There is no error in my input system.
And there is no error if I use the old input manager with the same code.
But when I merge the input system with the same code it causes problems.
I verified and I use Vector2 everywhere that I then convert to Vector3
The Vector3 is valid. The code does not produce any problem.
There is only the error message remaining.

Solved, I don’t know why
I had to check if the component that I was using was not null at RightClick() and LeftClick()

if (mapRenderer == null) return;
1 Like