Reloading the same screen taking minutes. But loading from another scene is instant.

I have a strange behavior occurring in my game, the first load from another scene is less than a second, but if the player dies, it trigger a SceneManager.LoadScene(“same scene”), and now the loading takes like 5 minutes, here an Image to illustrate:

alt text

I tried to load another scene that loads the main game again, but it just didn’t work:

alt text

Here is the trigger of the game over:

IEnumerator Death()
    {
        float ElapsedTime = 0;
        while (Time.timeScale > 0)
        {
            ElapsedTime += Time.fixedDeltaTime/5;
            Time.timeScale = Mathf.Clamp01(1f - ElapsedTime);
			if (Custom)
			{
				GameObject.Find("Difficulty_Script").GetComponent<AudioSource>().volume = Mathf.Clamp01(1f - ElapsedTime);
			}
			else
			{
				Music.volume = Mathf.Clamp01(1f - ElapsedTime);
			}			
			HUD.alpha = Mathf.Clamp01(1f - (ElapsedTime * 2));
			Blur.SetFloat("_Size", ElapsedTime * 2);

            yield return null;
        }

        ElapsedTime = 0;

        while (ElapsedTime < 1)
        {
            ElapsedTime += Time.fixedDeltaTime/5;
            GameOver.color = new Color(1,1,1, Mathf.Clamp01(0f + ElapsedTime));
            yield return null;
        }

		ElapsedTime = 0;

        while (ElapsedTime < 1)
        {
            ElapsedTime += Time.fixedDeltaTime /5;
            Fade.color = new Color(0, 0, 0, Mathf.Clamp01(0f + ElapsedTime));
            yield return null;
        }
        Time.timeScale = 1;
		SceneManager.LoadScene("Reloading");
			   		 
		yield return null;
    }

(Ignore all the Mathf, I’ll change it later :P)

Time.fixedDeltaTime is the timestep used for processing physics in FixedUpdate(). In order for the physics interactions to remain consistent, the physics rate must scale with Time.timeScale.

What you’re probably looking for is Time.unscaledDeltaTime.

When you die, you’re slowing down time over the course of ~5 seconds… However, that number of seconds is increasing with every new frame as you reduce the timeScale. If the elapsedTime is 0.95, for example, then your timeScale is 0.05 and Time.fixedDeltaTime should be reporting 0.001 (assuming the baseline 50 physics updates per second). The more time passes, the more time will be necessary to reach the end of the timer when you reset the scene.

Quite frankly, considering the loops you’re using, I think I’m more amazed that it actually CAN reach the end of all that. Is Time.fixedDeltaTime not actually reporting zero eventually?

That’s where Time.unscaledDeltaTime comes in. It’s specifically intended to ignore any changes to Time.timeScale so that this sort of thing won’t happen.