- Home /
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:
I tried to load another scene that loads the main game again, but it just didn't work:
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)
Answer by Eno-Khaon · Oct 15, 2019 at 02:21 AM
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.
I tried with Time.unscaledDeltaTime, but it's just the same, the code doesn't have a problem to reach Scene$$anonymous$$anager.LoadScene("Reloading"), the problem is when it starts loading, I put a debug.log("Hi") right before Scene$$anonymous$$anager.LoadScene("Reloading"), and he only print after 5 $$anonymous$$ and with like 250 "Hi".
If it's printing that ~250 times, what's your logic for calling the Death() function?
That makes it sound like it's queued up a scene load ~250 times simultaneously and is actually willing to go through with it.
I'm a shame as Programmer xD
if (Player.HP <= 0 && !DeathBlock)
{
DeathBlock = true; // that was false
StartCoroutine(Death());
StartCoroutine(BlurFade());
}
thank you very much and sorry