Quick answer: Subscribe in OnEnable; unsubscribe (-=) in OnDisable. Stacks otherwise.
Player toggles between menu and game. Each toggle re-enables the action. Jump fires twice, then thrice, then four times.
The Fix
PlayerActions _actions;
void Awake() { _actions = new PlayerActions(); }
void OnEnable() {
_actions.Player.Enable();
_actions.Player.Jump.performed += OnJump;
}
void OnDisable() {
_actions.Player.Jump.performed -= OnJump;
_actions.Player.Disable();
}
void OnDestroy() { _actions.Dispose(); }
Pair every += with -=. Dispose at end. C# event handler list grows otherwise.
Verifying
Toggle scenes 5 times. Jump fires once per press. Without unsubscribe: 5 fires per press.
“Subscribe pair. Unsubscribe pair. One callback.”
Related Issues
For Input composite, see composite. For control scheme, see scheme.
Pair subscribes. One callback.