Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to properly remove player with the New Input System?

Discussion in 'Input System' started by Xstyler, Feb 2, 2021.

  1. Xstyler

    Xstyler

    Joined:
    Nov 5, 2015
    Posts:
    9
    When I plug a new controller in the computer and I press any button on the controller, the OnPlayerJoined event is triggered and a new player is created.

    But, when I unplug the controller nothing happens. I assume the OnPlayerLeft event should be triggered, but it's not. Is this a bug?

    If I plug the controller back in and press any button, the OnPlayerJoined is triggered again, a new player is created and the first player loses the reference to the controller.

    I tried with the Invoke Unity Events and with Send Message.

    Code (CSharp):
    1. public void OnPlayerJoined(PlayerInput playerInput)
    2. {
    3.   print("player joined");
    4. }
    5.  
    6. public void OnPlayerLeft(PlayerInput playerInput)
    7. {
    8.   print("player left");
    9. }
    My second idea was to create a new event in PlayerInput on the DeviceLostEvent, to delete the player from the scene like this:

    Code (CSharp):
    1. public void PlayerDisconected(PlayerInput playerInput)
    2. {
    3.    GameObject[] players;
    4.    players = GameObject.FindGameObjectsWithTag("Player");
    5.    Destroy(players[playerInput.playerIndex]);
    6.  }
    This deletes the player from the list in PlayerInputManager under Debug and from the scene but I get errors:

    IndexOutOfRangeException: Index was outside the bounds of the array.
    (wrapper stelemref) System.Object.virt_stelemref_class_small_idepth(intpth,object)
    Assertion failed
    UnityEngine.InputSystem.LowLevel.NativeInputRuntime...


    Can someone please tell me how to properly remove the player?

    Thank you in advance.
     
  2. Oivin

    Oivin

    Joined:
    Jan 23, 2016
    Posts:
    4
    I've wrestled with this bug for the better part of a year, and very few people seem to come across it.
    I'm finally going to get to the bottom of it and will let you know if I find anything out.

    Up to now, I have used a workaround. It removes the errors when deleting PlayerInputs, but it's clunky, because it doesn't fix the fundamental issue and you have to relink users manually.
    What worked for me up to this point is going to line 1322 in InputUser and replace the if statement with this:

    Code (CSharp):
    1. if (s_AllUserData[userIndex].lostDeviceCount > 0)
    2.                             {
    3.                                 ArrayHelpers.EraseSliceWithCapacity(ref s_AllLostDevices, ref s_AllLostDeviceCount,
    4.                                     s_AllUserData[userIndex].lostDeviceStartIndex,
    5.                                     s_AllUserData[userIndex].lostDeviceCount);
    6.  
    7.                                 s_AllUserData[userIndex].lostDeviceCount = 0;
    8.                             }
    I noticed that if I open a fresh project I don't get this behavior; instead it relinks properly. That makes me believe I can get to the bottom of it. As I said I'll make sure to report back if I'm successful
     
    Last edited: Feb 7, 2021
  3. Oivin

    Oivin

    Joined:
    Jan 23, 2016
    Posts:
    4
    I finally managed to isolate this problem in my case. I'm not yet sure it solves all the problems mentioned, but it DOES solve that controllers lose their link with the PlayerInput:


    1. Select the InputActionAsset that is used by the Player Input that you spawn.

    prefab.png

    2. Select the control scheme that game pads will be assigned to, and edit it

    pads.png

    3. Set it to Required on Gamepad.
    require.png

    Now, if your control scheme setup is slightly different, you might have to do a slightly different fix. But this was where my problem stemmed from, which was identical to what you're describing.
     
    tmonestudio likes this.
  4. Xstyler

    Xstyler

    Joined:
    Nov 5, 2015
    Posts:
    9
    @Oivin thank you for the reply, but this didn't work for me.

    I think the problem is in the Input Actions.

    I updated the Unity and Input Package on the last version.
    Then I tried to create new Input Actions, but unfortunately, after the update, my player couldn't even join the game.

    I solved the problem by importing a demo sample from the Input package and setting that Input action on my player input. When I reconnected all the buttons, everything started working.
     
  5. Fressbrett

    Fressbrett

    Joined:
    Apr 11, 2018
    Posts:
    92