Hi,
I did the code below to make appear a joystick when I touch the left side of the screen : it’s functional but i’ve got a message error each time I touch the right side. How to delete this message error without change my solution?
Script:
Touch mytouch = Input.GetTouch (0);
//Multitouch adaptation
for(short i = 1; mytouch.position.x > (Screen.width / 2); i++)
{
mytouch = Input.GetTouch (i);
}
Error message :
ArgumentException: Index out of bounds.
ActiveButton.Update () (at Assets/Game/Scripts/ActiveButton.cs:29)
Input.GetTouch expect an index to be between 0 and Input.touchCount - 1
Your for loop doesn’t make sense at all. You’re generating strange, strange indices.
Try something like this, and adapt to get it working the way you want it.
// .......
Touch mytouch = default(Touch);
bool wasTouchedInRightArea = false;
//Multitouch adaptation
for(int i = 0; i < Input.touchCount; ++i)
{
mytouch = Input.GetTouch (i);
// If any touch is on the right side of screen
if (touch.position.x > (Screen.width / 2))
{
// Stop looking at the other touches
wasTouchedInRightArea = true;
break;
}
}
if (wasTouched)
{
switch (mytouch)
// .......