Search Unity

How can I unsubscribe from action callback?

Discussion in 'Input System' started by Windroid, Jan 22, 2020.

  1. Windroid

    Windroid

    Joined:
    Nov 28, 2017
    Posts:
    10
    I want to subscribe and unsubscribe to some action callbacks with simple method, so I wrote somethings like this:
    Code (CSharp):
    1.         private void InitializeInput()
    2.         {
    3.             owner.playerInput.actions["interact"].performed += OnInteract;
    4.             owner.playerInput.actions["interact"].canceled += OnNotInteract;
    5.         }
    6.  
    7.         private void DeinitializeInput()
    8.         {
    9.             owner.playerInput.actions["interact"].performed -= OnInteract;
    10.             owner.playerInput.actions["interact"].canceled -= OnNotInteract;
    11.         }
    12.  
    13.         private void OnInteract(InputAction.CallbackContext ctx) => Debug.Log("True");
    14.         private void OnNotInteract(InputAction.CallbackContext ctx) => Debug.Log("False");
    15.  
    the subscribe part which is InitializeInput() works well, but the unsubscribe part won't work.
    If I run InitializeInput() again, it run 2 times for each performed and canceled callback.

    Is there anythings wrong?
     
    Deleted User likes this.
  2. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    Any luck with finding out whats going on with this?

    I've done exactly as you have and managed to unsubscribe and it works well but i have one specific action that i cant unsubscribe from. its really strange. did you ever manage to fix this or find out what was causing the issue?