OnTouchDown script not working...

Hey all. Could you help me figure out why this doesn’t work?

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class OnTouchDown : MonoBehaviour
{
#if UNITY_IPHONE
	void Update () {
		RaycastHit hit = new RaycastHit();
		for (int i = 0; i < Input.touchCount; ++i) {
			if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
			
			Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
			if (Physics.Raycast(ray, out hit)) {
				hit.transform.gameObject.SendMessage("OnMouseDown");
		      }
		   }
	   }
	}
#endif
}

It works for one touch, then stops working… I can’t figure out why. I am using Unity 4. Thanks in advance! :slight_smile:

EDIT: It actually fires many times per touch. How can I fix this?

try something like this

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

public class OnTouchDown : MonoBehaviour
{
void Update () {
RaycastHit hit = new RaycastHit();
if(Input.touchCount >0){
for (int i = 0; i < Input.touchCount; ++i) {
//if c# if i am correct
Vector3 touch_Position = new vector (Input.GetTouch(i).position.x,Input.GetTouch(i).position.y,0)
Ray ray = Camera.main.ScreenPointToRay(touch_Position);
if (Physics.Raycast(ray, out hit)) {
hit.transform.gameObject.SendMessage(“OnMouseDown”);
}
}
}
}
}

Never mind I solved it. I just called the code once per second. Thanks anyway! :slight_smile: