- Home /
Input.GetJoystickNames gives incorrect index with multiple controllers
I do not know if this is a bug with the Xbox 360 controller drivers, or with Input.GetJoystickNames, but I fear it might be both.
Connecting more than 5 360 controllers exhibits some unusual behaviour:
Connecting up to 4 works as you would expect, and Input.GetJoystickNames works fine.
When connecting the 5th controller, controller LED is active, but it is still recognised by windows and Unity, seems reasonable enough.
Now, if any of the original 4 controllers disconnects, the 5th controller will automatically take the disconnected controller’s Joystick number, which is unusual, as normally controllers retain their number.
Unity acknowledges the disconnection of the controller, but not the Joystick number change in the 5th controller.
Furthermore, reconnecting the disconnected controller will assign it to the 5th joystick, but Unity will still have it identified as its original position in Input.GetJoystickNames, even though the 2 controllers have changed Joystick numbers.
So, weird bug in 360 controller drivers, but also a weird bug in Input.GetJoystickNames not updating array position with joystick number beyond 4 360 controllers.
Answer by p_025 · Jan 09, 2016 at 07:40 AM
The controller number as reported by Unity using GetJoystickNames() does not necessarily correspond to the player "quadrant" that lights up on the XBox 360 controller. If you have five 360 controllers connected to your computer at once, Windows is able to use all of them, but the controller driver won't be able to light up a quadrant on the fifth one. When one of the first four is disconnected, it lights up the available quadrant, but Unity is smart enough to know the actual controller it was keeping track of hasn't changed. Then when you reconnect the disconnected controller, the driver once again can't find an available player quadrant so it doesn't light up again, but Unity knows it's the same controller being reconnected, so it fills that spot in the GetJoystickNames() array again.
It kind of sucks that you can't depend on the correct quadrant lighting up on a 360 controller, but this behavior by Unity is actually somewhat helpful. Rather than shortening the joysticks array, it replaces the disconnected one with an empty string in the GetJoystickNames array, so you can detect a disconnect and prompt the user to reconnect it.
Your answer
Follow this Question
Related Questions
Unity ignoring joysticks of specified number 0 Answers
[XBOX][CONTROLLER][INPUT] Xbox controller "Console/Guide" button not mapped in unity 1 Answer
How to use both keyboard and xbox 360 controller for input with standard FPS controller? 0 Answers
Xbox controller joystick axis small movements? 1 Answer
How do you properly deal with analog joystick input on multiple controller inputs? 1 Answer