touchPhase.Ended/Began problem

Hello Everyone!

I have a problem with my JS. I’m working on a mobile app and i’d like to use Touches for detecting if the touch is held or if it’s ended by the time the collision happens.
Here’s my code:

function Update () {

var count = Input.touchCount;

if (trigger == true) { //Var trigger is controlled by OnTriggerEnter

 	for(var i : int = 0; i < count; i++)
 	{ 
		if (Input.GetTouch(i).phase == TouchPhase.Stationary)
		{	
			Debug.Log("Held");		
		}
 
		if (Input.GetTouch(i).phase == TouchPhase.Ended)
		{
			Debug.Log("Lifted");	
		}	
 	 }
 	 trigger = false;
  }
}

Stationary is working but the Problem is that if i’ve lifted my finger from the screen, count is 0 and the for loop doesn’t even start. I can’t find the workaround for that. Could you please help?

Thank You!

If you lift your finger you will get a TouchPhase.Ended for sure.

But after the first time you enter your if-block (presumably when the user starts touching) you set the trigger var to false. How should it ever become true again and therefore go into the loop?

Ok, I think the code was overcomplicated for my needs, so i changed it like this:

function Update () {

var count = Input.touchCount;

if (trigger == true && count > 0) {
	Debug.Log("Held");		
 	trigger = false;
  }

else if (trigger == true && count == 0){
  	Debug.Log("Lifted");
  	trigger = false;
  	
  }
}

It’s working now.