[C#] Index out of bounds

Hi!
I don’t know how to fix this errors:

  • ArgumentException: Index out of bounds.
    TouchManager.TouchInput (UnityEngine.GUITexture texture) (at Assets/_scripts/TouchManager.cs:8)
    ButtonMovement.Update () (at Assets/_scripts/ButtonMovement.cs:26)
  • ArgumentException: Index out of bounds.
    TouchManager.TouchInput (UnityEngine.GUITexture texture) (at Assets/_scripts/TouchManager.cs:33)
    ButtonMovement.Update () (at Assets/_scripts/ButtonMovement.cs:26)

Here are the scripts:
TouchManager:

using UnityEngine;
using System.Collections;

public class TouchManager : MonoBehaviour{
    public static bool guiTouch = false;
   
    public void TouchInput (GUITexture texture){
        if (texture.HitTest(Input.GetTouch(0).position)){
            switch (Input.GetTouch(0).phase){
            case TouchPhase.Began:
                SendMessage("OnFirstTouchBegan");
                SendMessage("OnFirstTouch");
                guiTouch = true;
                break;

            case TouchPhase.Stationary:
                SendMessage("OnFirstTouchStayed");
                SendMessage("OnFirstTouch");
                break;
           
            case TouchPhase.Moved:
                SendMessage("OnFirstTouchMoved");
                SendMessage("OnFirstTouch");
                break;
           
            case TouchPhase.Ended:
                SendMessage("OnFirstTouchEnded");
                guiTouch = false;
                break;
            }   
        }

        if (texture.HitTest(Input.GetTouch(1).position)){
            switch (Input.GetTouch(1).phase){
            case TouchPhase.Began:
                SendMessage("OnSecoundTouchBegan");
                SendMessage("OnSecoundTouch");
                break;
               
            case TouchPhase.Stationary:
                SendMessage("OnSecoundTouchStayed");
                SendMessage("OnSecoundTouch");
                break;
               
            case TouchPhase.Moved:
                SendMessage("OnSecoundTouchMoved");
                SendMessage("OnSecoundTouch");
                break;
               
            case TouchPhase.Ended:
                SendMessage("OnSecoundTouchEnded");
                break;
            }   
        }
    }
}

ButtonMovement:

using UnityEngine;
using System.Collections;

public class ButtonMovement : TouchManager {
    public enum type {LeftButton, RightButton, JumpButton};
    public type buttonType;
    public GameObject playerObject = null;
    public GUITexture buttonTexture = null;
    Rigidbody2D playerRigidbody = null;
    public float jumpHeight = 0.0f;
    public float moveSpeed = 0.0f;
    private Animator anim;
    GroundChecker check;
   
    void Start () {
        playerRigidbody = playerObject.GetComponent<Rigidbody2D>();
        check = playerObject.GetComponent<GroundChecker>();
        anim = playerObject.GetComponent<Animator> ();
    }
   
    void FixedUpdate (){
        anim.SetBool ("Grounded", check.grounded);
    }
   
    void Update () {
        TouchInput (buttonTexture);
    }
   
    void OnFirstTouchBegan (){
        if(check.grounded == true){
            switch (buttonType) {
            case type.JumpButton:
                playerRigidbody.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
                break;
            }
        }
    }
   
    void OnSecoundtTouchBegan (){
        if(check.grounded == true){
            switch (buttonType) {
            case type.JumpButton:
                playerRigidbody.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
                break;
            }
        }
    }
   
    void OnFirstTouch (){
        switch (buttonType) {
           
        case type.LeftButton:
            playerObject.transform.Translate(-Vector2.right * moveSpeed * Time.deltaTime);
            break;
           
        case type.RightButton:
            playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
            break;
        }
    }
   
    void OnSecoundTouch (){
        switch (buttonType) {
           
        case type.LeftButton:
            playerObject.transform.Translate(-Vector2.right * moveSpeed * Time.deltaTime);
            break;
           
        case type.RightButton:
            playerObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime);
            break;
        }
    }
}

On the button movement should you have that second t in line 40?

Void OnSecoundtTouchBegan()

You call TouchManager.TouchInput() on every frame via ButtonMovement.Update().

From TouchManager.TouchInput() you call Input.GetTouch(0)

I am guessing that GetTouch is accessing an array and you are attempting to gain access to the first touch. However, what if there are no touches at this moment? Then you would be trying to access the first item in a zero length array. Thus ArgumentException: Index out of bounds.

In the documentation, I see that they test Input.touchCount > 0 before trying to check GetTouch(0). You may want to do the same.

1 Like

Yeah, it works! Thank you for your help!

I have almost the same scripts, from Bud Broesky.
I have the same error again.
@Hadzik , can you upload your working script so I see what I’m doing wrong?