Game Development Reference
In-Depth Information
}
}
This function uses the
GetJoystickNames
function of the input, which gets and re-
turns an array of strings, which consists of the names of the connected gamepads.
We use this to set our Boolean to true or false; true meaning there's a device
connected and false meaning that the game couldn't detect a device. The reason
we use a try-catch expression is because if there is no gamepad connected
In-
put.GetJoystickNames()
will give you an
IndexOutOfRangeException
er-
ror.
Creating the identifier function
Our last step in creating the device detector will be to add the ability to identify the
gamepad connected. Add this function to the script, just below the
DetectCon-
troller
function:
void IdentifyController()
{
Controller = Input.GetJoystickNames()[0];
}
As you can see, we are assigning the name of the gamepad connected to our
Con-
troller
variable. To use this function, call it within the
DetectController
func-
tion, in the
if
statement, where we set
isControllerConnected
to
true
.