Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Reference Exceptions MissingReferenceException while executing 'started' callbacks

Discussion in 'Input System' started by slaga, Mar 19, 2021.

  1. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    Hello everyone , i get this error only while the scene is reloaded or my player dies and gets respawned

    Edit: my respawn is a simple level load function

    Code (CSharp):
    1. MissingReferenceException while executing 'started' callbacks of 'Gameplay/Shoot[/Keyboard/z]'
    2. UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    3. UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
    This happens only on key presses and not movement!

    And to be exact after each reloading of the scene i always get these errors even though the controls work
    Code (CSharp):
    1. MissingReferenceException: The object of type 'PlayerController' has been destroyed but you are still trying to access it.
    2. Your script should either check if it is null or you should not destroy the object.
    3. UnityEngine.MonoBehaviour.StartCoroutine (System.Collections.IEnumerator routine) (at <e5b75411c8cd4bfaa9e760be20206f29>:0)
    4. PlayerController.OnCharge (UnityEngine.InputSystem.InputAction+CallbackContext context) (at Assets/Scripts/Player/PlayerController.cs:728)
    5. 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.1.0-preview.3/InputSystem/Utilities/DelegateHelpers.cs:51)
    6. UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    7. UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
    8. ---------------------------------------------------------
    9. MissingReferenceException while executing 'canceled' callbacks of 'Gameplay/Charge[/Keyboard/z]'
    10. UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    11. UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
    12. ----------------------------------------------------
    13. MissingReferenceException: The object of type 'PlayerController' has been destroyed but you are still trying to access it.
    14. Your script should either check if it is null or you should not destroy the object.
    15. UnityEngine.MonoBehaviour.Invoke (System.String methodName, System.Single time) (at <e5b75411c8cd4bfaa9e760be20206f29>:0)
    16. PlayerController.MegaBuster () (at Assets/Scripts/Player/PlayerController.cs:887)
    17. PlayerController.FireWeapon () (at Assets/Scripts/Player/PlayerController.cs:874)
    18. PlayerController.OnShoot (UnityEngine.InputSystem.InputAction+CallbackContext context) (at Assets/Scripts/Player/PlayerController.cs:702)
    19. 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.1.0-preview.3/InputSystem/Utilities/DelegateHelpers.cs:51)
    20. UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    21. UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
    22. ------------------------------------------------------
    23. MissingReferenceException while executing 'started' callbacks of 'Gameplay/Shoot[/Keyboard/z]'
    24. UnityEngine.InputSystem.LowLevel.<>c__DisplayClass7_0:<set_onUpdate>b__0(NativeInputUpdateType, NativeInputEventBuffer*)
    25. UnityEngineInternal.Input.NativeInputSystem:NotifyUpdate(NativeInputUpdateType, IntPtr)
    26.  

    all of the code is in my player controller script and these are the initialization of the system

    Code (CSharp):
    1. void Awake()
    2.     {
    3.         // get handles to components
    4.         animator = GetComponent<Animator>();
    5.         col = GetComponent<BoxCollider2D>();
    6.         rb2d = GetComponent<Rigidbody2D>();
    7.         sprite = GetComponent<SpriteRenderer>();
    8.  
    9.         playerActionMap = inputMaster.FindActionMap("Gameplay");
    10.         pauseActionMap = inputMaster.FindActionMap("PauseMenu");
    11.         jump = playerActionMap.FindAction("Jump");
    12.         movement = playerActionMap.FindAction("Movement");
    13.         shoot = playerActionMap.FindAction("Shoot");
    14.         dash = playerActionMap.FindAction("Dash");
    15.         swap = playerActionMap.FindAction("Swap");
    16.         charge = playerActionMap.FindAction("Charge");
    17.         pause = playerActionMap.FindAction("Pause");
    18.  
    19.         pause.performed += OnPause;
    20.         pause.canceled += OnPause;
    21.         pause.started += OnPause;
    22.  
    23.         movement.performed += PlayerDirectionInput;
    24.         movement.canceled += PlayerDirectionInput;
    25.  
    26.         jump.performed += OnJump;
    27.         jump.canceled += OnJump;
    28.  
    29.         shoot.started += OnShoot;
    30.         shoot.performed += OnShoot;
    31.         shoot.canceled += OnShoot;
    32.  
    33.         charge.started += OnCharge;
    34.         charge.performed += OnCharge;
    35.         charge.canceled += OnCharge;
    36.  
    37.         dash.started += OnDash;
    38.         dash.performed += OnDash;
    39.         dash.canceled += OnDash;
    40.  
    41.         swap.performed += OnSwap;
    42.         swap.started += OnSwap;
    43.         swap.canceled += OnSwap;
    44.     }
    45.  
    46.     private void OnEnable()
    47.     {
    48.         movement.Enable();
    49.         jump.Enable();
    50.         shoot.Enable();
    51.         dash.Enable();
    52.         swap.Enable();
    53.         charge.Enable();
    54.         pause.Enable();
    55.         Debug.Log("PrintOnEnable: map was enabled");
    56.     }
    57.  
    58.     private void OnDisable()
    59.     {
    60.         movement.Disable();
    61.         jump.Disable();
    62.         shoot.Disable();
    63.         dash.Disable();
    64.         swap.Disable();
    65.         charge.Disable();
    66.         pause.Disable();
    67.         Debug.Log("PrintOnDisable: map was disabled");
    68.     }
     
  2. linojon

    linojon

    Joined:
    Aug 25, 2014
    Posts:
    118
    Try adding OnDestroy that does -= the onX handlers
     
  3. slaga

    slaga

    Joined:
    Dec 3, 2018
    Posts:
    142
    you mean the function OnDestroy?
     
  4. XuizElLoko

    XuizElLoko

    Joined:
    May 7, 2019
    Posts:
    1
    For anyone with this issue, @linojon answer is correct, but it could have been explanied a bit more xD
    For example, I have my code like this:
    Code (CSharp):
    1.    
    2. void Awake(){
    3.    jump = playerActionMap.FindAction("Jump");
    4.    movement = playerActionMap.FindAction("Movement");
    5. }
    6. void OnEnable(){
    7.    movement.performed += PlayerDirectionInput;
    8.    movement.canceled += PlayerDirectionInput;
    9.    movement.Enable();
    10.    jump.performed += OnJump;
    11.    jump.canceled += OnJump;
    12.    jump.Enable();
    13. }
    14. void OnDisable(){
    15.    movement.performed -= PlayerDirectionInput;
    16.    movement.canceled -= PlayerDirectionInput;
    17.    movement.Disable();
    18.    jump.performed -= OnJump;
    19.    jump.canceled -= OnJump;
    20.    jump.Disable();
    21. }
    22.  
    23.  
    (OnDisable is called before OnDestroy, so its fine like this)
     
    EMMinjung and Emtheory like this.