Input System Button Triggers Multiple Times
Hi guys. So I've been playing around with the new Input System and things are starting to get a little frustrating. My issue is button pushes are firing multiple times. I've tried multiple settings with the type of action (eg Button, Pass Through etc), and Interactions being set to press only, or release or whatever, doesn't seem to matter. It'll either fire 2 times or 4 times depending on the settings.
I've got multiple controllers here for testing and as if that wasn't confusing enough, if a second or more player joins in, their button pushes trigger only once. It's always only player one's buttons who trigger multiple times. Does anyone know how to fix this?
Looks like I'm not the only one, looking at this forum thread.
Here is my very simple, basic test code just for reference.
public void GetInteractValues( InputAction.CallbackContext value )
{
if ( value.started )
print("foo");
}
Answer by aDaxxas · Aug 07, 2020 at 09:34 AM
My solution was to add an if statement like this :
public void PickupObject(InputAction.CallbackContext context)
{
if (context.performed)
{
Debug.Log("PERFORMED");
}
}
I think that there is multiple context (started, canceled, performed...) and by default it's reacting to every state
No, this wasn't the solution. The "value.started" makes sure of that. It turns out the input system doesn't play well with prefab characters/players. Bug, if you ask me. I'll post the solution separately.
Commenting to this as this was one of the first hits on Google so i think its worthwhile dropping on here...
I believe you may have had 2 issues, and for the general case this may well be the solution.
The input system ABSOLUTELY triggers multiple event triggers per 'button' press for each of the "Phases" within its context.
E.g:
EventSystem is running OnReturned in context:{ action=Player/OnReturn[/Keyboard/enter] **phase=Started** time=8.6282363999926 control=Key:/Keyboard/enter value=1 interaction=UnityEngine.InputSystem.Interactions.PressInteraction }
EventSystem is running OnReturned in context:{ action=Player/OnReturn[/Keyboard/enter] **phase=Performed** time=8.6282363999926 control=Key:/Keyboard/enter value=1 interaction=UnityEngine.InputSystem.Interactions.PressInteraction }
EventSystem is running OnReturned in context:{ action=Player/OnReturn[/Keyboard/enter] **phase=Canceled** time=8.74305079999613 control=Key:/Keyboard/enter value= interaction=UnityEngine.InputSystem.Interactions.PressInteraction }
(**'s added to draw attention to the phase)
I was unable to replicate your other answer of having a prefab not in the scene triggering a second instance, or generating an input system by pre-fab generating a second instance... I could only guess that it may have been as a result of an 'addlistener' duplicating. Alternatively, it was a bug that has since been fixed in 2020.3.24f1.
Answer by LexGear · Aug 16, 2020 at 02:35 AM
Answer found after months of searching on another forum. Prefab triggers a second instance. This is obviously a bug. But here is how you make sure it doesn't fire twice. Check the scene for the gameobject in question before proceeding.
Checking for gameObject.scene.IsValid()
when processing the action callback appears to work around the issue, but a proper fix would certainly be preferred.