- Home /
WaitForSecondsRealtime issue during timescale change
I am trying to slow down the game temporary when my player picks up a buff item. I am doing it by starting the following Coroutine on collision:
public IEnumerator SlowMo () {
Time.timeScale = StaticVariables.Buff_SlowMo_Speed;
Overlay_SlowDown.SetActive(true);
Time.fixedDeltaTime = StaticVariables.fixedDeltaTime * Time.timeScale;
yield return new WaitForSecondsRealtime (StaticVariables.Buff_SlowMo_Duration);
Time.timeScale = 1;
Time.fixedDeltaTime = StaticVariables.fixedDeltaTime * Time.timeScale;
Overlay_SlowDown.SetActive(false);
}
StaticVariables holds
public static float Buff_SlowMo_Speed = 0.5f;
public static float Buff_SlowMo_Duration = 1;
The problem is, nothing after the yield function is triggered, and I have no idea why. Timescale does not return to normal and the overlay game object does not get disabled. I am using the timescale manipulation code from the unity examples. I honestly have almost no experience with the timescale manipulation and would be very grateful if someone pointed out the (probably quite dumb) mistake I am making.
Ok, kept testing, not only WaitForSecondsRealitme not working, WaitForSeconds isn't working either, and the Timescale manipulation isn't working either... No idea what the hell is going on. Will comment when I find out, maybe it helps someone .
Answer by Makiavel · Feb 16, 2021 at 11:07 AM
Ok, I am an idiot (duh). I forgot the difference between the Coroutines and Functions. If you call a function on another game object, you can destroy the object that called it as long as you don't need anything from it and the function does not return anything. Coroutines... , I completely forgot the object that called coroutines has to be present for the entire duration of it for the coroutine to finish. My object that called coroutine got destroyed after the collision and so the coroutine started and didn't finish. I simply moved the coroutine call into the object that isn't destroyed and instead of calling the coroutine directly, I call a simple function that calls the coroutine.
I glanced over your question yesterday and was about tp point out the common reasons why a coroutine may stop executing, one more time, though I didn't have any time. Great you figured it out yourself.
Your answer
Follow this Question
Related Questions
Pause menu doesn't pause everything 0 Answers
How to add Hit Stop Frames? 0 Answers
TimeScale = 0 crashes Unity 1 Answer
How to Pause game 1 Answer
How to return unity back to its normal time scale and fixedDeltaTime 0 Answers