argument exception index out of bounds error on line 40

using UnityEngine;
using System.Collections;

publicclassplayerTouch:MonoBehaviour
{
publicGameObjectbulletPrefab;
publicintHealth=0;

//Usethisfor initialization
voidStart()
{

}

//Updateiscalledonceper frame
voidUpdate()
{
Vector3mouseWorldPos=Camera.main.ScreenToWorldPoint(Input.GetTouch(1).position);
//Line 40^^
mouseWorldPos.z=0f;
transform.LookAt(mouseWorldPos);

if(Input.GetButtonDown(“Fire1”))
{
GameObjectbullet=GameObject.Instantiate(bulletPrefab);
bullet.transform.position=transform.position;
bullet.transform.forward=transform.forward;
}
}

voidOnTriggerEnter2D(Collider2DaCollider)
{
if(aCollider.gameObject.layer==LayerMask.NameToLayer(“Enemy”)){
Destroy(aCollider.gameObject);
//Destroy(gameObject);
}

if(aCollider.gameObject.tag==“Enemy”)
{
Health++;
Debug.Log(Health);

if(Health>=3)
{
//Destroy(gameObject);

}
}

}
}

can any one help??

You are calling Input.GetTouch(1) but what i the user does not touched the device Twice simultaneously ?
Then Input.GetTouch(1) is out of bound.
You should check that Input.touchCount > 0 before accessing the touch.
PS the touch array starts from 0 so 1 would be the second touch.

For a good example how to use the touch input class take look in the documentation

@fffMalzbier I’m sorry I do not quite understand where I should put that at. can you explain it a little more?

Thanks

Input.GetTouch(int) returns a specific Touch inside a Touch array. The int parameter determines the index in the array to return.

However, if the Touch array is of size 1, ie. the user is only touching the screen at one point, then Input.GetTouch(1) will try to access something that is outside of the array.

Add an if statement to check if Input.touchCount >= 2 first, to make sure that the Touch array is of at least size 2.