Search Unity

Resolved How do I fix callbacks to input events still persisting across scenes

Discussion in 'Input System' started by Chazzwazzler, Oct 12, 2022.

  1. Chazzwazzler

    Chazzwazzler

    Joined:
    May 2, 2018
    Posts:
    35
    upload_2022-10-12_11-58-49.png

    I was programming a settings menu in my game, and I noticed that when I switched from my main scene (the one with the player, the guns, etc) to my menu scene, the input system was still trying to call the functions that had subscribed to the input action events, even though they were subscribed in another scene.

    I'm pretty new with events, so please forgive my stupidity if this is something basic, but do functions not unsubscribe from an event when you load a different scene? And if they don't, how can I fix that? I don't want a whole bunch of errors and garbage piling up the further someone plays my game, because I have a bunch of scenes in there and I don't want a few hundred errors and failed function calls happening every time the player clicks.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    You have to unsubscribe events too. Good place is in OnDestroy, or use the OnEnable/OnDisable pair and make sure to remove all event handlers before adding new ones, just in case the method that adds event handlers gets called multiple times.
     
    Chazzwazzler likes this.
  3. Chazzwazzler

    Chazzwazzler

    Joined:
    May 2, 2018
    Posts:
    35
    I tried doing that already, but it didn't work. I found out the issue though while trying to test to see if I just wrote something wrong, and I did.
    For some reason removing the function from the event callbacks doesn't work when you have it structured like this:
    upload_2022-10-12_13-10-16.png
    upload_2022-10-12_13-11-16.png
    (I just copied it from a tutorial a long time back when I started writing my game and forgot to change it to the other form)

    But it only removes when it is like this:
    upload_2022-10-12_13-12-51.png
    upload_2022-10-12_13-13-4.png
    upload_2022-10-12_13-13-24.png

    I'll keep that in mind from the future, the reason why it was doing all the errors was because I had dashing, shooting, switching weapons, etc. written like this because I pretty much just copied and pasted the dash line of code and changed the function names on it.
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,975
    Yeah, you cannot unregister a lambda (anonymous function) callback by writing the same code again! That is a general C# thing because you end up having two different methods: one is assigned and another one is attempted to be removed, which ends up doing nothing.

    The correct way of doing that with lambdas would be to store the lambda method in a variable or field:
    Code (CSharp):
    1. var dashCallback = () => { Debug.Log("dash"); };
    2. Some.System.OnDashEvent += dashCallback;
    3. Some.System.OnDashEvent -= dashCallback;
    Of course just assigning the method is better practice since you avoid having this extra variable/field.