- Home /
Unity iPhone Touch Counter
I'm trying to create a counter to work on iPhone using Touch events on a primitive cube GameObject. I only want it to increment once per touch. As it is now, it reads the touch in milliseconds, so the update function it may Log 3 to 5 counts per update. Is there a "MonoBehaivor.OnMouseDown" for iOS Touches?
// attach the script to a primitive, then when tapped on iPhone it should increment a single integer per touch to the debug.log //
private var pickNum = 0;
function Update(){
if (Input.touchCount > 0){
var touchPosition:Vector2 = Input.GetTouch(0).position;
var ray : Ray = Camera.main.ScreenPointToRay (touchPosition);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 20.0)) {
transform.Rotate(0,0,10);
pickNum = Input.touchCount + 1;
Debug.Log("Screen Touched " + pickNum + " times");}
}
}
}
Answer by DaveA · Aug 01, 2011 at 04:15 AM
See this: http://answers.unity3d.com/questions/124198/onmousedown-alternative-for-mobile.html
Actually that code you posted is close. You know, touches have 'phases' so you can look for touch start, touch end, etc. Look for touch end, and increment your counter then.
Thanks DaveA. TouchPhase.Ended worked perfectly.
Your answer
![](https://koobas.hobune.stream/wayback/20220612163405im_/https://answers.unity.com/themes/thub/images/avi.jpg)