- Home /
Question by
aclee · Jul 26, 2014 at 07:25 PM ·
javascripterrortouch
Exception error
i am trying to create a script for detecting when a player touches the screen in cartian positions on android but when i touch one of the zones i get UnityException: Index out of bounds. help please
function Update(){
if (Input.touchCount == 1){
var touchOne = Input.GetTouch(1);
//LEFT
if(touchOne.position.x > 130 && touchOne.position.x < 180 && touchOne.position.y > 625 && touchOne.position.y < 675){
print("LEFT");
}
//RIGHT
if(touchOne.position.x > 230 && touchOne.position.x < 280 && touchOne.position.y > 625 && touchOne.position.y < 675){
print("RIGHT");
}
//UP
if(touchOne.position.x > 180 && touchOne.position.x < 230 && touchOne.position.y > 575 && touchOne.position.y < 625){
print("UP");
}
//DOWN
if(touchOne.position.x > 180 && touchOne.position.x < 230 && touchOne.position.y > 675 && touchOne.position.y < 725){
print("DOWN");
}
}
}
Comment
Best Answer
Answer by Landern · Jul 26, 2014 at 07:27 PM
Input.GetTouch is zero based, so the first one is Input.GetTouch(0), Input.GetTouch(1) would refer to the second touch.
i pretty much did, you are trying to access the wrong NU$$anonymous$$BER based off GetTouch method being zero based. You confirmed there was just 1 Touch with the line:
if (Input.touchCount == 1){
but just after that you checked for the second touch by:
var touchOne = Input.GetTouch(1);
it should be:
var touchOne = Input.GetTouch(0);
because the first touch in the array/collection is actually access by 0. This is displayed in the documentation for Input.GetTouch and it's examples