Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Need help with managing Player Input using 2 gamepads and 2 separate canvases for separate players

Discussion in 'Input System' started by Dr_Sausages, Feb 1, 2023.

  1. Dr_Sausages


    Jan 30, 2023
    Sorry if this is a repost but I've been struggling with this for a few days now and no matter what I try I run into some issue or another

    Looking for some help in setting up a Character Selection Screen for a Beat Em Up (e.g. Streets of Rage) style game

    Using Gamepad Controls

    Scene is simple:
    P1 Canvas - has a grid with 3 buttons on it for the 3 characters to choose from
    P2 Canvas - has a grid with 3 buttons on it for the 3 characters to choose from
    Background Image
    Game Manager
    > Player Manager (Has Player Input Manager to join Player prefabs)
    >Instantiated Players (only testing with 2 players so far)​

    To make it easier the Game Manager is also keeping track of Instantiated Players within a List
    Code (CSharp):
    1. public List<GameObject> Players = new List<GameObject>();

    The Player Prefab has a script to keep track of lives etc.
    For modules it has...
    Multiplayer Event System
    Input System UI Input
    Player Input​

    The P1 and P2 canvases are initially set to not active. They are set active when a player joins the game

    When a player is instantiated though it does not have any UI elements selected

    And here's the problem

    If P1 canvas has buttons

    and P2 canvas has buttons

    When I activate a canvas I try to select the first button on it. This works fine and the red button is selected when P1's canvas activates

    But when I activate a button on my P2 Canvas using Character1.Select() this seems to unselect the button on my P1 Canvas

    I thought I could get around this issue by adding a simple script to my P1 and P2 canvases to try to confirm which Player was meant to be interacting with it

    I give the script a
    public PlayerInput input;

    Then when that canvas is initialised I try to pass one of the Players InputSystemUIModules to it
    Code (CSharp):
    1.  input.uiInputModule = gameManager.Players[_index].GetComponentInChildren<InputSystemUIInputModule>();
    But get the error
    NullReferenceException: Object reference not set to an instance of an object

    I'm lost as this point. I don't know the best way to do this

    I've dug around for days looking for a simple way to manage 2 players that each have gamepads on the screen and each have their own separate canvas to move on

    Is there a way to set a specific player to select a specific button on one of the canvases?

    Or am I going about this the wrong way?

    Can anyone point me in the right direction?

    Attached Files:

  2. Dr_Sausages


    Jan 30, 2023
    happy for this thread to be closed
    didn't realise when I had the player join event I had selected the Static Join Player rather than the Dynamic one