Search Unity

Resolved New Input System - Strange Missing Reference Exception on Exit

Discussion in 'Input System' started by Kajih, Dec 14, 2020.

  1. Kajih

    Kajih

    Joined:
    Jul 27, 2012
    Posts:
    2
    Hi All,
    I've been learning the new input system for my latest project and I have come to a strange exception error whenever the game stops or I switch scenes. Now it doesn't appear to actually cause any issues, but I dislike errors I don't fully understand and I fear it may cause issues down the line.

    so the setup is fairly basic,I have an Action Map setup named cursor_actions and an Action named Movement set to Pass Through and Stick. I have an inputManager object and class which has the basic code for input:

    Code (CSharp):
    1. using UnityEngine.InputSystem;
    2.  
    3. public class InputManager : MonoBehaviour, Input_actions.ICursor_actionsActions
    4. {
    5.     private Input_actions controls;
    6.  
    7.     private void Awake()
    8.     {
    9.         controls = new Input_actions();
    10.         controls.cursor_actions.SetCallbacks(this);
    11.     }
    12.  
    13.     private void OnEnable() { controls.Enable(); }
    14.     private void OnDisable() { controls.Disable(); }
    15.  
    16.  
    17.     public void OnMovement(InputAction.CallbackContext context)
    18.     {
    19.         switch (context.phase)
    20.                 {
    21.                     case InputActionPhase.Performed:
    22.                         Vector2 leftStickInputforCursor = context.ReadValue<Vector2>();
    23.                         cursorVelocity = new Vector3(leftStickInputforCursor.x * cursorSpeed, 0, leftStickInputforCursor.y * cursorSpeed);
    24.                         break;
    25.                     case InputActionPhase.Canceled:
    26.                         cursorVelocity = Vector3.zero;
    27.                         break;
    28.                 }
    29.     }

    I omitted the movement code because the error seems to appear regardless. everything works just fine, but when I move to a new scene or a stop the game this exception appears:


    MissingReferenceException while executing 'canceled' callbacks of 'cursor_actions/Movement[/XInputControllerWindows/leftStick]'
    UnityEngine.InputSystem.InputActionAsset:Disable()
    Input_actions:Disable() (at Assets/Input/input_actions.cs:217)
    InputManager:OnDisable() (at Assets/Scripts/Monobehaviors/InputManager.cs:26)


    and also:


    MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    Your script should either check if it is null or you should not destroy the object.
    UnityEngine.GameObject.GetComponent[T] () (at <d815b7efac424eeb8e053965cccb1f98>:0)
    InputManager.OnMovement (UnityEngine.InputSystem.InputAction+CallbackContext context) (at Assets/Scripts/Monobehaviors/InputManager.cs:70)
    UnityEngine.InputSystem.Utilities.DelegateHelpers.InvokeCallbacksSafe[TValue] (UnityEngine.InputSystem.Utilities.InlinedArray`1[System.Action`1[TValue]]& callbacks, TValue argument, System.String callbackName, System.Object context) (at Library/PackageCache/com.unity.inputsystem@1.0.0/InputSystem/Utilities/DelegateHelpers.cs:51)
    UnityEngine.InputSystem.InputActionAsset:Disable()
    Input_actions:Disable() (at Assets/Input/input_actions.cs:217)
    InputManager:OnDisable() (at Assets/Scripts/Monobehaviors/InputManager.cs:26)


    It looks like the input system's callback is still firing AFTER unity has deleted my InputManager object or something. I'm a bit confused here because how can I check if itself is null before using it? lol I am probably not understanding the error correctly here, or that I am simply not using the system correctly or I am missing either a null check or not cleaning up my scene properly when it unloads and loads a new scene.

    Any help here to understand this error and why it's occurring would be helpful. Thanks.
     
  2. Kajih

    Kajih

    Joined:
    Jul 27, 2012
    Posts:
    2
    I seem to have resolved the issue. I was looking at it the wrong way, I was assuming that the deleted object that was being referenced was the inputManager class but was in fact another object I was referencing from within one of the Callback context functions.