Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Bug Actions not always being properly removed.

Discussion in 'Input System' started by fanto_ep, Aug 27, 2022.

  1. fanto_ep


    Mar 7, 2015
    I'm writing this in hopes I'm wrong, but I think I've run into a case that seems to be causing re-registering action calls OnEnable, mainly because it seems as though .remove functionality isn't being correctly executed, or something to that effect.

    Maybe I'm wrong and there is a simple solution to this, but I have yet to find it searching around.

    Code (CSharp):
    2. void OnEnable() {
    3.     Button.clicked += () => {
    4.         RunSomeFunction(true);
    5.          Variable = newValue;
    6.         SetPosition(NewPos);
    7.     };
    8. }
    9. void OnDisable() {
    10.     Button.clicked -= () => {
    11.         RunSomeFunction(true);
    12.         Variable = newValue;
    13.         SetPosition(NewPos);
    14.     };
    15. }
    This duplication of registering the clicked events on the OnEnable function happens because the OnDiable function isn't properly unregistering those events.

    It's happening in several scripts, so it's not an isolated issue or typo in one specific script, and I am in fact Debug.Log to make sure the OnDisable function is being registered.

    I'm using UIElements built-in the 2021.3.6f1 Engine Version and InputSystem 1.3.0;

    The only solution I can come up with at the moment that seems to work is registering the .clicked event to a specific function, and unregistering it:

    Code (CSharp):
    2. void OnEnable() {
    3.     Button.clicked += RunSomeNewFunction;
    4. }
    5. void OnDisable() {
    7.     Button.clicked -= RunSomeNewFunction;
    8. }
    9. void RunSomeNewFunction(){
    10.         RunSomeFunction(true);
    11.         Variable = newValue;
    12.         SetPosition(NewPos);
    13. }
    This works, condensing all of these different function calls into separate functions that do not receive any parameters. Then calling that function in the .clicked event triggers. That seems like a bit of over complicating the code, for certain actions require varying inputs, so I would essentially need to create many different functions to handle the same 4 lines of code with different input parameters;