Detect joypad is connect or unplugged

Hi, is it a way to know if a pad is connected or unplugged during game? For example, I play with the joypad and after few time unplugged the pad. Now, I would a message warning on screen that the pad is unplugged…

is it possible?

Thanks!

The only information you can get about attached joysticks in Unity is the Input.GetJoystickNames() string array of attached joysticks. You would have to check this array every 1-2 seconds on a timer to determine if anything has changed.

Hi Guavaman, thanks for your answer, but Input.GetJoystickNames don’t resolve my problem, because Unity cached Input.GetJoystickNames information, so, when I unplugged the joypad, I sill every the information inside the buffer.

There is a way to force unity to refresh the Input.GetJoystickNames informations?

thanks

No there isn’t. That’s the only piece of information Unity gives you about what joysticks are attached and you cannot force it to refresh if it isn’t changing.

GetJoystickNames is not cached. It will update when you remove the joystick, but you have to call the Input.GetJoystickNames again to get a new array because it is generated each time you call the function.

GetJoystickNames has a lot of different issues many platforms Unity supports. For example, the array index of the joystick name does not align to the Unity joystick id on some platforms. On some platforms when a joystick is plugged in or removed, the array elements collapse changing all the joystick id’s of all attached joysticks, while on others the array fills empty spaces with “”. It’s very inconsistent. In addition, hot-plugging does not work consistently across different platforms either.

Because of these (and many more) problems with UnityEngine.Input, I decided to start from scratch and write Rewired, an advanced input system that gets input directly from the OS on many platforms so I could have functional, predictable, and reliable joystick input that would work for multi-player games with reliable hot-plugging. If you’re interested in an input system that just works, please check out the free trial of Rewired.

Oh, thanks for the informations :slight_smile:
May be that in Unity3d editor, Input.GetJoystickNames do not works because cached the Input.GetJoystickNames, is it possbile? I don’t have try in release mode for now…

I have worked with Unity’s input system on just about every platform it supports and it does not cache Input.GetJoystickNames on any platform, in the editor or in builds.

OSX does have some major issues in the editor though. Controllers cannot be hot plugged in the editor after a certain Unity 5 version introduced a new bug. The joystick must be plugged in before the editor is launched in order for it to be recognized by Unity. This was not the case in older versions of Unity. This bug was reported to them many versions ago, but as far as I am aware it has not been fixed yet. This issue does not affect Rewired which implements its own native input solution on OSX.

Thanks!
however, I use build in “PC, Mac Standalone” in a iMac.

If the editor is running on OSX, the build target doesn’t matter. OSX editor has a bug with joystick hot-plugging.

ah ok, thanks :slight_smile: now I am more relaxed :slight_smile: