Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved NullReferenceException while executing 'performed' callbacks

Discussion in 'Input System' started by jiexdrop, Jan 10, 2021.

  1. jiexdrop

    jiexdrop

    Joined:
    Oct 24, 2019
    Posts:
    7
    Hello. I'm having this error with the new input system. I can't figure out what my problem is.
    Here is my error:
    Code (CSharp):
    1. NullReferenceException while executing 'performed' callbacks of 'Player/Shoot[/Mouse/leftButton]'
    2. UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    3. UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
    4.  
    Here is my code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class Collision : MonoBehaviour
    8. {
    9.  
    10.     private MapRenderer mapRenderer;
    11.  
    12.     private Controls controls = null;
    13.  
    14.     private Controls Controls
    15.     {
    16.         get
    17.         {
    18.             if (controls != null) { return controls; }
    19.             return controls = new Controls();
    20.         }
    21.     }
    22.  
    23.     // Start is called before the first frame update
    24.     void Awake()
    25.     {
    26.         mapRenderer = GetComponent<MapRenderer>();
    27.     }
    28.  
    29.     private Vector2 mousePos;
    30.  
    31.     private void SetMousePos(Vector2 mousePos)
    32.     {
    33.         this.mousePos = mousePos;
    34.     }
    35.  
    36.     private void RightClick()
    37.     {
    38.         if (mousePos == null) return;
    39.  
    40.         Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y));
    41.         Vector3 savePosition = position;
    42.         // Perform RightClick()
    43.     }
    44.  
    45.     private void LeftClick()
    46.     {
    47.         if (mousePos == null) return;
    48.  
    49.         Vector3 position = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y));
    50.         Vector3 savePosition = position;
    51.         // Perform LeftClick(
    52.      }
    53.  
    54.     private void OnEnable()
    55.     {
    56.         Controls.Enable();
    57.         Controls.Player.Look.performed += ctx => SetMousePos(ctx.ReadValue<Vector2>());
    58.         Controls.Player.Shoot.performed += _ => RightClick();
    59.         Controls.Player.Shoot.performed += _ => LeftClick();
    60.     }
    61.  
    62.     private void OnDisable()
    63.     {
    64.         Controls.Disable();
    65.     }
    66.  
    67. }
    68.  
    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.
     
    Last edited: Jan 10, 2021
  2. jiexdrop

    jiexdrop

    Joined:
    Oct 24, 2019
    Posts:
    7
    Solved, I don't know why
    I had to check if the component that I was using was not null at RightClick() and LeftClick()
    Code (CSharp):
    1. if (mapRenderer == null) return;
     
    mowax74 likes this.