Do Slowmotion effect for x seconds

Hello! I am currently working on my first 2d game and everytime when the player dies i want a slowmotion to appear for a few seconds. I didnt figure out how i can have a function for a certain amount of time. Can someone help me with the script? The trigger of the slowmo will be an if method

 public float slowdownfactor;
 public float slowdownlength;

 public gameobject player;

 public void start()
 {
    if (player.activeSelf = false)
    {
          Time.timeScale = slowdownfactor;
    }
 }

…and so on. Now i just need help so set the slowdown length. I hope someone can help me:)
~dvxl

You can use a coroutine:

public void start()
{
    if(player.activeSelf == false)
    {
        StartCoroutine(WaitThenRestoreTime();
    }
}
private IEnumerator WaitThenRestoreTime()
{
    Time.timeScale = slowdownFactor;
    yield return new WaitForSecondsRealtime(slowdownTime);
    Time.timeScale = 1f;
}

This calls the WaitThenRestoreTime coroutine function with StartCoroutine. The coroutine will then activate the slowdown effect, wait a few seconds in real-time (as to not be affected by the reduced timeScale), and then return timeScale to normal. This is possible because coroutines run simultaneously to other methods, and can be “paused” in place using methods like WaitForSeconds. For a more in-depth explanation of coroutines, check out Unity - Manual: Coroutines

Oh, and just a quick reminder to use “==” instead of “=” when comparing values.

I hope this was helpful!