The input from any controller fires the input event from every controller

Hello!

My problem
I’m trying to make a simple couch coop game but every time, someone pushes a button on any controller, the input action from every player gets triggered.

The setup
I have a gameObject with a playerInputManager component attached:
7215361--867364--playerInputManager.png

The player prefab has a playerInput component:
7215361--867367--player-prefab.png
I also added my own playerController script to it.

playerController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class playerController : MonoBehaviour
{
    Input controls;
    public boxController boxController;


    [SerializeField]
    int playerNr;

    void Awake()
    {
        controls = new Input();
        controls.threeBoxes.boxLeft.performed += context => inputForwarder("boxLeft");
        controls.threeBoxes.boxMiddle.performed += context => inputForwarder("boxMiddle");
        controls.threeBoxes.boxRight.performed += context => inputForwarder("boxRight");
 
    }

    private void Start()
    {
        boxController = GameObject.Find("threeBoxes").GetComponent<boxController>();

        playerNr = gameObject.GetComponent<PlayerInput>().splitScreenIndex;

    }

  ]void inputForwarder(string whichInput)
    {
        boxController.inputReceiver(whichInput, playerNr);
    }

    private void OnEnable()
    {
        controls.threeBoxes.Enable();
    }

    private void OnDisable()
    {
        controls.threeBoxes.Disable();
    }
}

boxController.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;

public class boxController : MonoBehaviour
{

    public void inputReceiver(string whichInput, int playerNr)
    {
       Debug.Log(whichInput+" "+playerNr);
    }

}

And if press any button on one of the controllers, I get the Debug.Log message two times (always first from the first player, then from the second one, regardless of who pushed the button).

I absolutely don’t get why that happens … do you have any idea, what the problem could be?

Thank you very much! :slight_smile:

I kind of solved it without really understanding why …

Instead of writing down every input in the code like

controls.threeBoxes.boxLeft.performed += context => inputForwarder("boxLeft");

like it was done in the most tutorials I watched, I used the inspector instead as in this video:

. In the player input component, under the headline “Events” I found every input action I defined in the input action asset.
When I add a callback context in a script attached to the player prefab like

public void BoxLeftForwarder(InputAction.CallbackContext context)
    {
        Debug.Log("Test");

    }

when I add my player prefab in the inspector I can choose the new method and it works:
7233425--870617--events-in-inspector.png

Now, every input is only triggered by the player, who did it.

When my method wants to start a method in another script and I want to know, from which gamepad it came, I can add gameObject.GetComponent<PlayerInput>().splitScreenIndex
to have an unique identifier, where that come from.

Only open question for me at the moment is:
When I press something on the first controller, it fires two times - one for splitScreenIndex -1 and one for splitScreenIndex 0 - I don’t get where the -1 comes from …