Massive lag spike on first Input.GetTouch with nothing on scene but one script (Android)

I was having problems with one of my projects every time I pressed for first time the screen. At first I thought it was caused by a script that disabled an UI Text but after one day of researching I couldn’t find any answer so I decided to try recreating the problem in an empty scene. The results are that whenever I place a script that checks for Input.GetTouch(0), the first time I touch the screen, the game freezes for a fraction of a second even if the scene is completely empty. I took a look at the profiler and the script checking for the input was generating that lag spike. Note that after the first touch everything works smoothly and without any kind of lag spike.

This was the script used
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Scr_Input : MonoBehaviour
    // Update is called once per frame
    void Update()
        if (Input.touchCount > 0)
            Touch touch = Input.GetTouch(0);

            if (touch.phase == TouchPhase.Began)

And here’s the profiler when I first touch the device’s screen.
The device I tested on is a Xiaomi MI A1 but I had this kind of problem before using a Samsung Galaxy S9.

Now you may notice that the lag spike is generated by the Debug.Log() code but It just doesn’t make sense to me since in my other projects I don’t have any Debug.Log() going on and there are other things happening before I touch the screen so there is no other reason for the lag spike that the Input.GetTouch().
Also, in the picture you may see smaller spikes, those are the touches after the first one, just to prove that it only happens on the first touch.

So if you know how to avoid this I would be really thankful since it’s completely ruining the game experience because the first touch when you open the game will always generate a lag spike no matter the device.


Since I’ve read that Debug.Log() is expensive for perfomance I’ve changed it and placed a transform.position += new Vector3(1, 0, 0);

And as expected the lag spike isn’t that massive but even though the first touch is still generating a bigger lag spike than the ones after that and if you consider a bigger script (as the ones in my other project), the lag spike gets way too big and you can tell that the game lags at the first touch.

Here’s the Profiler

Okay I found a solution which works for me. It is split into 2 parts:

1.Trigger a drag start via code. It might be the only part you need to “fix” this issue. This is how I did it:

void Start() {
    // Trigger drag start via code to prevent initial drag delay on android
    PointerEventData pointer = new PointerEventData(EventSystem.current);
    ExecuteEvents.Execute(gameObject, pointer, ExecuteEvents.beginDragHandler);

For maybe obvious reasons: gameObject should be the the object which should receive the drag.

2.Set the “Scripting Backend” to “IL2CPP” in the “Player Settings” for Android.

This worked for me without step 1 in an empty scroll rect scene. In a more complex scene I also had to do step 1.

Regarding IL2CPP: as far as I have understood it from various posts, IL2CPP uses ahead of time compilation (AOT) in contrast to the default mono scripting backend which uses just in time compilation (JIT). The result for me with IL2CPP turned on is a faster app and also a reduced apk size (saved around 10 MB, in my case). Downside is that building takes longer (as it compiles ahead of time) than it would take with Mono. So you might only want to do this when actually releasing your game (depending how long building actually takes and if you want to deal with it during development).

There are even more pros & cons, so for a more detailed information check this post: Unity Connect

I hope this helps someone else!


I am actually experiencing the same issue on android even with the native unity scrollrect in an otherwise empty scene. Is there anyone out there who can help with this issue? Any unity folks/pros?

Same kind of problem here but with no perceived lag or freezing. It’s just something I noticed observing the profiler.

My demo/game starts fine with 0 physics queries, then the first touch input on screen activates a couple (jumping between 1 and 2 to be exact) unexplained physics queries that will stay there for all the duration of the demo. I Noticed they disappear whenever I deactivate the main camera, but I don’t understand how they can be related to it, will investigate better tomorrow.

To add a bit of background, it’s true that I have a raycast system that casts a ray on each touch input, but the same “fixed queries” behaviour presents itself even when that system is inactive.

After some more testing and observations I found these fixed queries problem is linked to the touch system input system only, and only noticeable when I profile the demo from the “simulator view” mode then (the onMouseClick behaviour in “game view” works normally instead activating a single physics query which then resets automatically back to 0, same that I was expecting to see from the touch input system).

Some clarity of what could be the cause of this unexplained behaviour would be more than appreciated!