I am a beginner with no coding background.
I made a joystick on screen with UI planes. The joysticks works perfectly but gives the following error:
ArgumentException: Index out of bounds.
UnityEngine.Input.GetTouch (Int32 index) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/InputBindings.gen.cs:564)
PlayerControl.Update () (at Assets/Scripts/Player/PlayerControl.cs:82)
Here is a part of the code which includes line 82 of the error, which is here line 27. this part of the code is called in the Update methode.
/* uiJoystickR control input for movement.
* (Input.touchCount starts with 1 as the first touch).
* (Input.GetTouch it's index starts with 0 as the first touch). */
if (Input.touchCount != 0)
{
// Look for each touch on the screen.
foreach (Touch _touch in Input.touches)
{
// Distance of the touch calculated from the center of the uiJoystickRPos.
float _dist = new Vector3(_touch.position.x - uiJoystickRPos.x,
_touch.position.y - uiJoystickRPos.y).magnitude;
// Reads if this touch in Input.touches is with in the uiJoysRRad radius.
if (_dist < uiJoysRRad + 20)
{
uiJoystickRTouchID = _touch.fingerId;
}
}
}
// Checking the current touchID of the touch controlling the UIJoystickR.
Debug.Log("uiJoystickRTouchID:" + uiJoystickRTouchID);
// When nothing is touching the uiJoystickR the uiJoystickRTouchID gets reseted back to -1 (-1 as no ID).
if (uiJoystickRTouchID != -1)
{
if (Input.GetTouch(uiJoystickRTouchID).phase == TouchPhase.Canceled ||
Input.GetTouch(uiJoystickRTouchID).phase == TouchPhase.Ended)
{
uiJoystickRTouchID = -1;
}
}
I can repeat the error most of the time by touching the screen with multiple fingers with in the joystick borders.
When I look at the value of the uiJoystickRTouchID when the error appairs it’s evrytime a random value equal to or above 0 which is as expected. What I didn’t expect is that it sometimes gives the error that it’s out of bounce in the Input.GetTouch index.
I hope I have provided you with enough information and that someone can help me with this error.