where should i put the heavy code ?

hello every one . so i have a piece of code that take some minutes to finish executing and i’ve no problem with that . the problem is when i run the game on a mobile device it sometimes crach and quit the application . i have no problem to make the player wait till it finish but i think unity doesn’t agree . i’ putting the code in the Start() method . is there any other way to do it + i would like to have a progress value to use it in the UI

NOTE : the script is slow because it have a big for loop

Hi, Unity doesn’t have any built in solution for this type of multithreading.
You should try to use Coroutines and yield return a WaitForEndOfFrame. This will give back the control for Unity so you can update the UI for progress value and the device won’t think this application is in a infinite loop. For example:

    public void Do()
    {
        StartCoroutine(StartTask());
    }

    private IEnumerator StartTask()
    {
        int iterations = 10000;
        long l = 0;
        for (int i = 0; i < iterations; i++) 
        {
            for (int y = 0; y < 25; y++)
                for (int z = 0; z < 25; z++)
                    l++;
            disp.text = $"{i} of {iterations}";
            yield return new WaitForEndOfFrame();
        }
        disp.text = "Done";
    }