Search code examples
c#unity-game-engineinput

Unity Input System Button Triggers Multiple Times


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");
 }

Solution

  • As LexGear responded here https://stackoverflow.com/a/63459803/5277266, checking if the gameObject is in valid scene is one solution, another one is to check if the gameObject is active :

    public void Move(InputAction.CallbackContext ctx)
    {
        // to avoid prefab dispatching the event...
        if (!gameObject.activeInHierarchy) // or if (!gameObject.scene.IsValid())
        {
            return;
        }
    
        // do things with the event
    }
    

    In my case, i did that check in the awake method to avoid doing it on each input event. I can do that only because i register C# event to dispatch the input event to multiple objects :

    void Awake()
    {
        // to avoid prefab dispatching the event...
        if (!gameObject.activeInHierarchy)
        {
            return;
        }
    
        OnMove += playerController.Move;
    }