Hello. I want to destroy an object if the player taps on it with their finger. I have attached he following script to the object (I got it from here) and I am running into a problem. The object is destroyed whenever I touch the screen at all (regardless of where I touched the screen in relation to the object). What am I doing wrong? Thanks
function Update () {
// Code for OnMouseDown in the iPhone. Unquote to test.
var hit : RaycastHit;
for (var i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
// Construct a ray from the current touch coordinates
var ray = camera.main.ScreenPointToRay (Input.GetTouch(i).position);
if (Physics.Raycast (ray,hit)) {
hit.transform.gameObject.SendMessage("OnMouseDown");
Destroy(gameObject);
}
}
}
}