Index Out of Bounds Error
I continue to get an index out of bounds error in my console. I don't know what's causing this. It is specifically this line - if(Input.GetTouch (0).deltaPosition .x<0). I tried adding if(Input.GetTouch (0).deltaPosition .x<0 && Input.GetTouch>0) in both if functions, but this did not work.
Here's my code (located in the Update Function):
if(!PausePanel.isPaused && mySpriteRenderer != null)
{
if(Input.GetTouch (0).deltaPosition .x<0)
{
// flip the sprite
mySpriteRenderer.flipX = true;
}
if (Input.GetTouch (0).deltaPosition.x > 0) {
// flip the sprite
mySpriteRenderer.flipX = false;
}
}
Answer by jdean300 · Jul 08, 2016 at 08:44 AM
It's because there are no touches currently, so Input.GetTouch(0)
is trying to get the touch in the first index of an empty array. You're going to want to check the touch count:
if(!PausePanel.isPaused && mySpriteRenderer != null && Input.touchCount > 0)
{
if(Input.GetTouch (0).deltaPosition .x<0)
{
// flip the sprite
mySpriteRenderer.flipX = true;
}
if (Input.GetTouch (0).deltaPosition.x > 0) {
// flip the sprite
mySpriteRenderer.flipX = false;
}
}
Answer by tqkiettk10 · Jul 08, 2016 at 09:10 AM
try to edit if(Input.GetTouch (0).deltaPosition .x<0 && Input.GetTouch>0)
to
if(Input.touchCount >0 && Input.GetTouch (0).deltaPosition .x<0)
you need checking touch count before check input.GetTouch(0). and you wrong "Input.GetTouch
"
Your answer
![](https://koobas.hobune.stream/wayback/20220612084436im_/https://answers.unity.com/themes/thub/images/avi.jpg)