[SOLVED] Code problem involving: Input.GetTouch (error "ArgumentException: Index out of bounds.")

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.

My guess would be that it’s triggered when you (A) press down outside joystick, then (B) press down on joystick, then (C) release your first press. Is that accurate?

Input.touches is an array that will grow and shrink as you touch and release. In the example of, at (A), it has 1 element (index 0). At (B), it has 2 elements, with the joystick finger being at index 1. After (C), what used to be at index 0 has ended, and then .touches has only 1 element; the joystick finger has moved to index 0. You cannot rely on a given finger being at the same index between frames.

You CAN rely on .fingerId to be the same for a given touch. Track your fingers using that instead, and you should be able to keep accurate track of the finger no matter what else is going on.

2 Likes

Thanks for your quick responds it helped me understand the problem.
[Solution] I now set uiJoystickRTouchID = _touch instead of using the Input.GetTouch(uiJoystickRTouchID). The UI joystick works no perfectly without any errors.