Coroutines and WaitForSeconds not working?

So I have a script I’m trying to use currently which is attached to an object with an UI canvass attached to it for a pause screen. As part of the Pause screen UI there are a bunch of buttons on a canvas with one of the buttons being selected. I wanted to have the text color and size of the selected button to be red and larger then the rest. I noticed that when closing the pause screen (by disabling the canvas) the size of the last selected button was staying larger then it should like it was still selected though. So I thought, hey I’ll just run the “Normal” animation pn the buttons before disabling the canvas and that should fix the problem. Since I have to wait for the normal animation to end before disabling the canvas I used a coroutine to allow me to wait until after the animation was over before tryng to disable the canvas. Only problem is it didn’t work. several hours of trouble shooting later it looks like Unity gets to the line 'yield return new WaitForSeconds(animClip.length); ’ and then just ignores the rest of the coroutine. To be precise it pauses fine but doesn’t exit the pause screen at all. So the function works once and then for some reason no longer functions. chuckles

Heres my current code.

public class PauseScreen : MonoBehaviour {

public bool isPaused;
public GameObject PauseCanvas;
public int arraySizeY;
public GameObject[] UIElements;
public Animator anim;
public AnimationClip animClip;
public int currentlySelectedObjectY;
public bool horizontalLastFrame;

void Start () {
	arraySizeY = 6;
	currentlySelectedObjectY = 0;

}
 
void Update () {

	if (isPaused) {
		PauseCanvas.SetActive (true);
		Time.timeScale = 0;
		anim = UIElements[currentlySelectedObjectY].GetComponentInChildren<Animator>();
		if (Input.GetAxisRaw ("Vertical") != 0 && horizontalLastFrame == false) 
		{
			currentlySelectedObjectY = currentlySelectedObjectY + (-1 * (int) Input.GetAxisRaw ("Vertical"));
			if (currentlySelectedObjectY < 0) {
				currentlySelectedObjectY = arraySizeY - 1;
			}
			if (currentlySelectedObjectY >= arraySizeY) {
				currentlySelectedObjectY = 0;
			}
			anim.Play ("Normal");
			anim = UIElements[currentlySelectedObjectY].GetComponentInChildren<Animator>();
			anim.Play ("Highlighted");
		}
		if (Input.GetAxisRaw ("Vertical") != 0) {
			horizontalLastFrame = true;
		} else {
			horizontalLastFrame = false;    
		}
	} else {
		PauseCanvas.SetActive (false);
		Time.timeScale = 1;
	}
	if (Input.GetKeyUp (KeyCode.Escape)) {
		StartCoroutine(WaitForPauseScreenReset());
	}

}

public IEnumerator WaitForPauseScreenReset () {
    //used to be more to this function but for the sake of trouble-shooting I got rid of it.  
	yield return new WaitForSeconds(animClip.length); //This is the line causin issues.
	Debug.Log ("wakka"); //gets printed when we pause intiall but not when we try to unpause the game
	currentlySelectedObjectY = 0;
	isPaused = !isPaused;

}

You put the Time.timeScale to zero so WaitForSeconds won’t work.
I give you a script I found on the net to use.

using UnityEngine;
using System.Collections;

public static class CoroutineUtil
{
    public static IEnumerator WaitForRealSeconds(float time)
    {
        float start = Time.realtimeSinceStartup;
        while (Time.realtimeSinceStartup < start + time)
        {
            yield return null;
        }
    }
}

Then just use:

        yield return StartCoroutine(CoroutineUtil.WaitForRealSeconds(1f));

Not sure if that helps but did you try to Debug.Log(animClip.length) before your yield?
Maybe the value is very high which is why it is seems to not be proceeding to the next lines.