Search Unity

Question Unity game lags on Android because of async function

Discussion in 'Editor & General Support' started by Mushegh_Khachatryan, May 17, 2023.

  1. Mushegh_Khachatryan

    Mushegh_Khachatryan

    Joined:
    Jul 2, 2021
    Posts:
    2
    I am currently making a checkers bot, which has to think for 2 seconds and make a move. What my code does, is it evaluates position over and over with increasing depth, until 2 seconds pass and then ends async function. But game sometimes freezes for more than 2 seconds during evaluating process and this only happens on android, in editor everything is ok. Does anyone have an idea what can cause the issue?
    Unity version:2022.2.2f1
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    If it does that without returning or yielding, NOTHING else is happening, not even your time update.

    Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

    Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

    Absolutely NOTHING will happen... until your code either:

    - returns from whatever function it is running

    - yields from whatever coroutine it is running

    As long as your code is looping, Unity isn't going to do even a single frame of change. Nothing.

    No exceptions.

    "Yield early, yield often, yield like your game depends on it... it does!" - Kurt Dekker
     
  3. Mushegh_Khachatryan

    Mushegh_Khachatryan

    Joined:
    Jul 2, 2021
    Posts:
    2
    Thanks for answering, but the problem is that what you said is correct for synchronous functions, and my bot does everything in async function and code will wait for that async function to finish using await. As I understand it shouldn't lag as it has nothing to do with main thread. And also if I did something wrong with making that function asynchronous, then it would create a huge lag everytime bot made a move, but lags happen only on android and in editor game waits for 2 seconds without lags(I have a clock animation running during bots thinking process and that helps to understand if game is lagging or not).