Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Input System' started by dim_wes, Jun 7, 2021.

  1. dim_wes

    dim_wes

    Joined:
    Oct 18, 2020
    Posts:
    5
    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:
    playerInputManager.png

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

    playerController.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class playerController : MonoBehaviour
    7. {
    8.     Input controls;
    9.     public boxController boxController;
    10.  
    11.  
    12.     [SerializeField]
    13.     int playerNr;
    14.  
    15.     void Awake()
    16.     {
    17.         controls = new Input();
    18.         controls.threeBoxes.boxLeft.performed += context => inputForwarder("boxLeft");
    19.         controls.threeBoxes.boxMiddle.performed += context => inputForwarder("boxMiddle");
    20.         controls.threeBoxes.boxRight.performed += context => inputForwarder("boxRight");
    21.  
    22.     }
    23.  
    24.     private void Start()
    25.     {
    26.         boxController = GameObject.Find("threeBoxes").GetComponent<boxController>();
    27.  
    28.         playerNr = gameObject.GetComponent<PlayerInput>().splitScreenIndex;
    29.  
    30.     }
    31.  
    32.   ]void inputForwarder(string whichInput)
    33.     {
    34.         boxController.inputReceiver(whichInput, playerNr);
    35.     }
    36.  
    37.     private void OnEnable()
    38.     {
    39.         controls.threeBoxes.Enable();
    40.     }
    41.  
    42.     private void OnDisable()
    43.     {
    44.         controls.threeBoxes.Disable();
    45.     }
    46. }

    boxController.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class boxController : MonoBehaviour
    7. {
    8.  
    9.     public void inputReceiver(string whichInput, int playerNr)
    10.     {
    11.        Debug.Log(whichInput+" "+playerNr);
    12.     }
    13.  
    14. }

    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! :)
     
  2. dim_wes

    dim_wes

    Joined:
    Oct 18, 2020
    Posts:
    5
    I kind of solved it without really understanding why ...

    Instead of writing down every input in the code like
    Code (CSharp):
    1. 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
    Code (CSharp):
    1. public void BoxLeftForwarder(InputAction.CallbackContext context)
    2.     {
    3.         Debug.Log("Test");
    4.  
    5.     }
    when I add my player prefab in the inspector I can choose the new method and it works:
    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
    Code (CSharp):
    1. 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 ...