Question by
crypticscott · Sep 10, 2017 at 02:20 AM ·
touchtouchscreenindexoutofbounds
ArgumentException: Index out of bounds. Help?
Current script, I am trying to add a touch feature to add money.
public class AddReducePlayerMoney : MonoBehaviour
{
public int money;
public GameObject cam;
void Update()
{
Touch myTouch = Input.GetTouch(1);
Touch[] myTouches = Input.touches;
for(int i = 1; i < Input.touchCount; i++)
{
if (Input.GetButtonDown("Fire1"))
{
cam.GetComponent<PlayerMoney>().addMoney(5);
}
if (Input.GetButtonDown("Fire2"))
{
cam.GetComponent<PlayerMoney>().subtractMoney(5);
}
}
}
}
Comment
Answer by Cuttlas-U · Sep 10, 2017 at 07:16 AM
hi; by "Input.GetTouch(1)" u mean that u want to get the second touch of the player; if u want to get the first one u should use Input.GetTouch(0) "
but the problem with your error is you are not checking if there is any touch in the game or not so first of all u should add this checking to your update method;
if ( Input.touchCount > 0)
Your answer
![](https://koobas.hobune.stream/wayback/20220612134532im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help with touch button (can't call a function) 0 Answers
Separate Input from Touch on GUI 0 Answers
Disabling aiming towards Joystick on first input. 0 Answers
How touch works 1 Answer