Massive Touch Lag on mobile devices?

Hi guys, I have this real problem with Touch Screen Lag on all the devices…

After a while of playing my game, the Touch sensing will suddenly lag greatly, resulting in me being unable to interact with the game properly. Strangely enough, the game still runs as smoothly as before…
—> Is there any way I can fix this Touch screen lag? Its a major problem in the game, and we can’t get it out unless we solve this…

Also, when we export it to the iPad, it somehow lags alot more than on the iPhone or the Android…
—> Is there a reason for this? And is there a way we can fix it?

Please help me, its exceedingly important…

This is the code which makes the touch work by the way.

function iPhoneUpdate(){
// If there are any touching on the screen

// Look for new touches
	for (var i:Number = 0; i < Input.touchCount; i++){
		var theTouch:Touch = Input.GetTouch(i);
		if (!touchEvents.Contains(theTouch.fingerId)){
			touchEvents.Add(theTouch.fingerId,new touchEvent(theTouch.position,theTouch.tapCount));
			var touchRay = mainCamera.ScreenPointToRay(theTouch.position);
			if (Physics.Raycast(touchRay,hit)){
				if (hit.transform.tag == "Roach"){
					var newTouchedObject:touchedObject = new touchedObject();
					newTouchedObject.theObject = hit.transform.gameObject;
					newTouchedObject.pointStarted = theTouch.position;
					theTouchEvent = touchEvents[theTouch.fingerId];
					theTouchEvent.theTouchedObject = newTouchedObject;
				}
			}
		}
		else {
			theTouchEvent = touchEvents[theTouch.fingerId];
			theTouchEvent.checked = true;
			theTouchEvent.currentPosition = theTouch.position;
		}
	}
	
	
	textToWrite = "";
	
	// Update current touch events
	for (var theTouchID in touchEvents.Keys){
		
		var theTouchEvent:touchEvent = touchEvents[theTouchID];
		
		// If that touch has ended
		if (theTouchEvent.checked == false){
			
			
			
			// If anything was touched
			if (theTouchEvent.theTouchedObject){
				
				// Update last position
				theTouchEvent.theTouchedObject.pointEnded = theTouchEvent.currentPosition;
			
				// If the touch was very short
				if (theTouchEvent.currentTime < tapTime){
					// TOUCH PHASE WAS A TAP
					NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Squash",theTouchEvent);
				
				}
				// If it wasn't short
				else {
					// TOUCH PHASE WAS A FLICK
					NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Flicked",theTouchEvent);

				}
			}
			
			
		}
		
		// If that touch has not ended yet
		else{
			
			// For tracing
			//textToWrite += theTouchID +" : " + theTouchEvent.currentTime + "

";

			// Increase timer for touch
			theTouchEvent.currentTime += Time.deltaTime;
			
			
			
		}
	}
	
	// Delete old touch events
	
	var newTouchEvents:Hashtable = touchEvents.Clone();
	
	for (var theTouchIDRemove in touchEvents.Keys){
		
		theTouchEvent = touchEvents[theTouchIDRemove];
		
		if (theTouchEvent.checked == false){
			newTouchEvents.Remove(theTouchIDRemove);
		}
		
	}
	
	touchEvents = newTouchEvents;
	
	
	for (var theTouchIDDirty in touchEvents.Keys){
		theTouchEvent = touchEvents[theTouchIDDirty];
		theTouchEvent.checked = false;
	}
	
	/*
	var theGUIText:GUIText = transform.GetComponent(GUIText);
	if (textToWrite != ""){
		theGUIText.text = textToWrite;
	}
	else {
		theGUIText.text = "Nothing here";
	}*/

}

P.S Strangely enough, on the Android, the game runs alot smoother at the start, but after around 5-10 minutes, the touch screen lag gets to such a terrible point I can’t even play… On the iPhone3GS, it runs not as smooth, but takes around 10-15 minutes for the lag to be too terrible to play…

EDIT

Fixed up the code a bit, here it is.

function iPhoneUpdate(){
// If there are any touching on the screen

// Look for new touches
	for (var i:Number = 0; i < Input.touchCount; i++){
		theTouch = Input.GetTouch(i);
		if (!touchEvents.Contains(theTouch.fingerId)){
			touchEvents.Add(theTouch.fingerId,new touchEvent(theTouch.position,theTouch.tapCount));
			touchRay = mainCamera.ScreenPointToRay(theTouch.position);
			if (Physics.Raycast(touchRay,hit)){
				if (hit.transform.tag == "Roach"){
					newTouchedObject = new touchedObject();
					newTouchedObject.theObject = hit.transform.gameObject;
					newTouchedObject.pointStarted = theTouch.position;
					theTouchEvent = touchEvents[theTouch.fingerId];
					theTouchEvent.theTouchedObject = newTouchedObject;
				}
			}
		}
		else {
			theTouchEvent = touchEvents[theTouch.fingerId];
			theTouchEvent.checked = true;
			theTouchEvent.currentPosition = theTouch.position;
		}
	}
	
	
	textToWrite = "";
	
	// Update current touch events
	if (touchEvents.Count > 0){
		for (var theTouchID in touchEvents.Keys){
			
			theTouchEvent = touchEvents[theTouchID];
			
			// If that touch has ended
			if (theTouchEvent.checked == false){
				
				
				
				// If anything was touched
				if (theTouchEvent.theTouchedObject){
					
					// Update last position
					theTouchEvent.theTouchedObject.pointEnded = theTouchEvent.currentPosition;
				
					// If the touch was very short
					if (theTouchEvent.currentTime < tapTime){
						// TOUCH PHASE WAS A TAP
						NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Squash",theTouchEvent);
					
					}
					// If it wasn't short
					else {
						// TOUCH PHASE WAS A FLICK
						NotificationCenter.DefaultCenter().PostNotification(theTouchEvent.theTouchedObject.theObject,"Flicked",theTouchEvent);

					}
				}
				
				
			}
			
			// If that touch has not ended yet
			else{
				
				// For tracing
				//textToWrite += theTouchID +" : " + theTouchEvent.currentTime + "

";

				// Increase timer for touch
				theTouchEvent.currentTime += Time.deltaTime;
				
				
				
			}
		}
		
		// Delete old touch events
		
		newTouchEvents = new Hashtable();
		
		for (var theTouchIDRemove in touchEvents.Keys){
			
			theTouchEvent = touchEvents[theTouchIDRemove];
			
			if (theTouchEvent.checked == true){
				theTouchEvent.checked = false;
				newTouchEvents.Add(theTouchIDRemove,touchEvents[theTouchIDRemove]);
			}
			
		}
		
		touchEvents = newTouchEvents;
	}

}

You are creating a large amount of garbage in this code, with cloning the hashtable, creating new objects and similar things.

Due to that, it will once it hits the memory limit of the device, start to lag considerably, as the garbage collection will start to eat all frametime at worst.
If you own iOS Pro / Android Pro you can see that in the Profiler as “Overhead”, on iOS you can alternatively also enable the in code profiler that prints to the console, this one will show it directly as time for the garbage collecting.

Can anyone help with the touch lag issue…?

I have the same problem,even if using a empty scene!!!