Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Can't remove InputAction subscribed functions from destroyed objects

Discussion in 'Input System' started by khnivlam, May 9, 2023.

  1. khnivlam

    khnivlam

    Joined:
    Jun 12, 2019
    Posts:
    4
    I have two scripts:
    ControllerReference.cs that doesn't get destroyed on load and has this reference to a button:
    public InputActionReference ToggleMap;

    and a MapController.cs that subscribes to ToggleMap at SceneLoaded:


    Code (CSharp):
    1.   private void Awake()
    2.         {
    3.             SceneManager.sceneLoaded += OnSceneLoaded;
    4.         }
    5.  
    6.         private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
    7.         {
    8.             _controllerRef.ToggleMap.action.performed += ToggleMap;
    9.          
    10.         }
    It also unsubscribes to ToggleMap in OnDestroy:


    Code (CSharp):
    1.  private void OnDestroy()
    2.         {
    3.             _controllerRef.ToggleMap.action.performed -= ToggleMap;
    4.         }
    There is one MapController in each scene, and inside ToggleMap() i only have a debug.log. When i go to scene 2 from scene 1 and press the ToggleMap button, i get 2 of the log instead of 1. and if i reference anything inside ToggleMap() such as debug.log(gameobject.name) it gives me nullref.

    The problem seems to be that -=ToggleMap doesn't do anything,and i get multiple += ToggleMaps that doesn't go away even though the object with MapController.cs was destroyed in the scene change.

    Please Help.
     
  2. khnivlam

    khnivlam

    Joined:
    Jun 12, 2019
    Posts:
    4
    Somehow replacing _controllerRef which was set in OnEnable to be = ControllerReference.Instance, with just ControllerReference.Instance solved the problem. So by not using the stored variable in _controllerRef, but using what the _controllerRef would store directly?

    Instead of _controllerRef.ToggleMap.action.performed += ToggleMap;
    I now have ControllerReference.Instance.ToggleMap.action.performed += ToggleMap; and that works.