Hello! I have a QTE challenge in my game that registers my inputs to a function to see if they are of the correct type. While adding the event callback with the
action.performed += context => function()
works, removing it again with
action.performed -= context => function()
doesn’t seem to do anything and the function still triggers when I press the button. How can I solve this issue? Here’s my code:
private InputAction jumpAction;
void BindPlayerKeyPresses() {
jumpAction = playerControlsRef.controls.FindAction("Jump");
Assert.IsNotNull(jumpAction);
jumpAction.performed += context => OnButtonPress(PlayerKeyPressType.JUMP);
}
void UnbindPlayerKeyPresses() {
jumpAction.performed -= context => OnButtonPress(PlayerKeyPressType.JUMP);
Debug.Log("removed bindings?");
}
Since you’re using a lambda, you’re trying to remove a different instance of the lambda than you added.
Two options - save your lambda to a variable and reuse that, or use a direct method reference. For example:
private InputAction jumpAction;
void JumpHandler(InputAction.CallbackContext ctx) {
OnButtonPress(PlayerKeyPressType.JUMP);
}
void BindPlayerKeyPresses() {
jumpAction = playerControlsRef.controls.FindAction("Jump");
Assert.IsNotNull(jumpAction);
jumpAction.performed += JumpHandler;
}
void UnbindPlayerKeyPresses() {
jumpAction.performed -= JumpHandler;
Debug.Log("removed bindings?");
}
OR
private InputAction jumpAction;
Action<InputAction.CallbackContext> jumpHandler;
void BindPlayerKeyPresses() {
jumpAction = playerControlsRef.controls.FindAction("Jump");
Assert.IsNotNull(jumpAction);
jumpHandler = ctx => OnButtonPress(PlayerKeyPressType.JUMP);
jumpAction.performed += jumpHandler;
}
void UnbindPlayerKeyPresses() {
jumpAction.performed -= jumpHandler;
Debug.Log("removed bindings?");
}
1 Like