A cleaner way to detect Joystick connect

I am using joyArray = Input.GetJoystickNames ();//Placeholder for future reference
if (joyArray[0] != “”){
useMouse = false;
However when the Joystick is disconnected and the empty string vale is true I get a run time out of range error.
The code works but I would like a cleaner way to test for Joystick being connected.

Check the length of the array rather than the first element.

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.Length);
if (names.Length == 19)
{
print(“PS4 CONTROLLER IS CONNECTED”);
PS4_Controller = 1;
Xbox_One_Controller = 0;
}
if (names.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
 }
 }