• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Davste · Feb 18, 2012 at 08:46 AM · c#detectiongamepad

How can I detect if a gamepad is present?

I need to detect if currently connected. From there, I will remove the second player if it isn't.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by rbisso · Oct 15, 2012 at 10:01 PM

On PC, you can use Input.GetJoystickNames(), which returns an array of strings of the names of the joysticks that are connected. If that array is empty, there are no pnp joysticks connected, I guess.

I hope that helps!

I'm currently trying to figure out how to tell if there is a Bluetooth joystick connected on an Android device, but that information doesn't seem to be quite as handily available.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image GeekyTime · Sep 14, 2014 at 05:39 AM 0
Share

I'd guess that if your gamepad isn't showing up in Input.GetJoystickNames(), it is registering itself as a bluetooth keyboard to try to get better compatibility with older versions of Android.

For whatever that's worth 2 years later...

avatar image Braun0 · May 11, 2015 at 05:43 PM 1
Share

rbisso, did you ever find a solution to your issue: "I'm currently trying to figure out how to tell if there is a Bluetooth joystick connected on an Android device"?

I am seeking an answer to the same question.

avatar image
2

Answer by robpuk38 · Nov 28, 2016 at 02:32 PM

you can do this in C#

 private int Xbox_One_Controller = 0;
     private int PS4_Controller = 0;
 void Update()
 {
 string[] names = Input.GetJoystickNames();
         for (int x = 0; x < names.Length; x++)
         {
             print(names[x].Length);
             if (names[x].Length == 19)
             {
                 print("PS4 CONTROLLER IS CONNECTED");
                 PS4_Controller = 1;
                 Xbox_One_Controller = 0;
             }
             if (names[x].Length == 33)
             {
                 print("XBOX ONE CONTROLLER IS CONNECTED");
                 //set a controller bool to true
                 PS4_Controller = 0;
                 Xbox_One_Controller = 1;
 
             }
         }
 
 
 if(Xbox_One_Controller == 1)
 {
 //do something
 }
 else if(PS4_Controller == 1)
 {
 //do something
 }
 else
 {
 // there is no controllers
 }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mvlsilva · Sep 13, 2017 at 01:14 AM 0
Share

Amazing soluction! But if i have two PS4 controllers conected in same time, or two Xbox One controllers? how could it differentiate?

avatar image Stanlyhalo · Nov 21, 2018 at 02:48 AM 0
Share

Whats the names[x].Length == A Keyboard?

avatar image
1

Answer by Lloyd_RedironLabs · Jan 18, 2016 at 06:32 PM

FYI, on PC I'm finding some GamePads (MOGA for example) stay registered as connected in Windows 10 --- even months later. And generate an Axis value if -1,-1.

Not sure if that helps anyone else. But I'll probably create an exception where until a joysticks value changes for the first time it continues to consider them disabled.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

chronological order of collision to give a winner unity 2d 4.3 1 Answer

Raycast causes all enemies to attack 1 Answer

Create a method that gets called everytime something happens, passing a Transform component to it 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges