Home > Software design >  Unity WaitForSecondsRealtime doesn't working when time.timescale = 0
Unity WaitForSecondsRealtime doesn't working when time.timescale = 0

Time:09-23

I have an animation that I want to play after my character dies. I'm trying to activate the Game Over screen after this animation is complete, but it doesn't work after yield return new WaitForSecondsRealtime(3f);

Here is my code:

private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "DeathArea")
        {
           StartCoroutine(Dead());
        }
    }


IEnumerator Dead() 
    {   
        animDie.SetActive(true);
        animDeath.SetTrigger("Die");
        Time.timeScale = 0;
        yield return new WaitForSecondsRealtime(3f);
        animDie.SetActive(false);
        isDead = true;
        deathScreen.SetActive(true);
        managerGame.Medal();
    }

Thank you!

CodePudding user response:

The coroutine is firing off, and Time.timescale is surely breaking things. I'd use it very sparingly if I were you, as this setting also persists between scenes.

CodePudding user response:

Please change WaitForSecondsRealtime(3f) to WaitForSeconds(3f)

result yield return new WaitForSeconds(3f);

  • Related