Search Unity

Question Switch player to another PlayerInput?

Discussion in 'Input System' started by florianhanke, Jan 23, 2021.

  1. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Hi all!

    I'm trying to allow people to jump in with their favourite input device (looking like the device, and moveable) and then select a player by moving onto it in a lobby.

    Problem is I don't know the best way to switch a player's PlayerInput.

    What I have when the input device collides with the chosen player:
    Code (CSharp):
    1. playerChooserInstance.playerInput.enabled = false;
    2. var targetPlayerInput = targetPlayer.GetComponent<PlayerInput>();
    3. targetPlayerInput.enabled = true;
    And that works surprisingly well since often the player loses the input, but will instantly pick up the newly enabled PlayerInput.

    But this is not a clean or stable solution. I'd love to be able to simply switch the input to the new PlayerInput and disable the old one. Any ideas?