Time Slowdown

Hi. Now I have a problem with time slowdown. When I change Time.timeScale to 0.0f or to 1.0f, I have not problems. But, if I want to decrease it gradually, it doesn’t work.
I have tried several approaches. For example:

//--------------some code--------------------
    if (pause)
    {
        while (Time.timeScale > 0.0f)
            {
              Time.timeScale = Time.timeScale - 0.1f;
              StartCoroutine(Slowdown());
            }
    }
    else {Time.timeScale = 1.0f;}

//--------------some code--------------------

    IEnumerator Slowdown()
    {
       yield return new WaitForSeconds(0.1f);
    }

or this:

//--------------some code--------------------
if (pause) 
{
  for (int i = 1;i<=10;i++)
  {  
    Invoke("Slowdown",0.1f);  
  }
}
else {Time.timeScale = 1.0f;}
//--------------some code--------------------
void Slowdown()
{
  Time.timeScale -=  0.1f;	//42nd line	  
}

But they didn’t bring a result. Last approach causes this error:

Time.timeScale is out of range. Needs to be between 0 and 100. UnityEngine.Time:set_timeScale(Single)
Pause:Slowdown() (at Assets/Standard Assets/Scripts/Pause.cs:42)

I think timeScale drop below zero. But why?
Help me, please.

There are a number of issues here. When Time.timeScale is set to 0.0, you do continue to get Update() calls, but Time.deltaTime will be 0.0. You will not get FixedUpdate() calls, and any WaitForSeconds() calls are scaled as well, so use of WaitForSeconds() in an IEnumerator will not work correctly. Time.time also stops. There is one value you can still use: Time.realtimeSinceStartup. Here is a coroutine that will scale time. If you want to go from 1.0 down to 0.0 in 3.0 seconds, you would call it:

StartCoroutine(ScaleTime(1.0f, 0.0f, 3.0f);

You call this once, not repeatedly in Update().

IEnumerator ScaleTime(float start, float end, float time) {
	float lastTime = Time.realtimeSinceStartup;
	float timer = 0.0f;
	
	while (timer < time) {
		Time.timeScale = Mathf.Lerp (start, end, timer / time);
		timer += (Time.realtimeSinceStartup - lastTime);
		lastTime = Time.realtimeSinceStartup;
		yield return null;
	}
	
	Time.timeScale = end;	
}

The while loop does not wait for the WaitForSeconds() function just because you call StartCoroutine(Slowdown())inside of it. Put the while loop inside of the IEnumerator instead, i.e. replace this:

if (pause)
{
	while (Time.timeScale > 0.0f)
	{
		Time.timeScale = Time.timeScale - 0.1f;
		StartCoroutine(Slowdown());
	}
}

with

if (pause)
{
	StartCoroutine(Slowdown());
}

IEnumerator Slowdown() {
	while (Time.timeScale > 0.0f)
	{
		Time.timeScale = Time.timeScale - 0.1f;
		yield return new WaitForSeconds(0.1f);
	}	
}

Also, change WaitForSeconds(0.1f); to something higher, waiting only 0.1 seconds is probably not going to look like a gradual change.