Unity Freezing

Hello, I have a ‘for’ loop in my coroutine who cause a Unity freezing. I’ve tried to fix it, but I don’t understand… Anybody has an idea why the loop provokes a freeze?

Thanks !

The coroutine :

  public static IEnumerator ScoreCountTo(int score, int target)
        {
            Debug.Log("Score Count ");
            int duration = 3;
            int start = score;
            float progress = 0f;
            string str = LanguageManager.Instance.readValue("txt_score");

            updateField(0, str + score);

            for (float timer = 0; timer < duration; timer += Time.deltaTime )
            {
                Debug.Log("Timer " + timer + " Time.deltaTime " + Time.deltaTime);
               // progress = timer / duration;
                //score = (int)Mathf.Lerp(start, target, progress);
                //updateField(0, str + score);
				yield return null;
            }
            updateField(0, str + target);
            MainSingleton.Instance.stopTime();
        }

Last result before the freeze :
72574-sans-titre.png

Note: Whenever something takes too long e.g. loops, etc. it causes unity to freeze.

Now coming to your answer:

Time.deltatime = 0.007 as shown in the screenshot. Lets consider it average Delta time. Which means you are adding 0.007 in your “timer” variable.

3 / 0.007 = 428.5 which means this loop will take 428.5 frames to complete. If frame rate per second is lets say 40, then it will take 428/40 = 10.7 seconds to complete this loop.

So my guess is that your loop is taking so long which makes unity freeze.