yield return new WaitForSeconds seems to work once, but then fails to work again.

So, I am working on a simulator game called Preschool Teacher Simulator, which is basically just a choice/physics/alternate endings game. I have been going fine script-wise, and have not encountered any issues, until today.

While writing my Headache script, (I know it’s messy ill clean it up later) I found that my yield return new WaitForSeconds(30); worked once, and then when the player completes a task, the script goes back and skips over it. Everything else works fine. Please note I am not done with the script, but I feel as if I cannot work any further with this error.

The part of the script I am working on is supposed to generate a number from 1 to 10 and play out a situation in the game accordingly. Between each situation there is a 30 second break, where people can do other things in the game like collect items that will be useful for later, or try to find an alternate ending. Even though the initial 30 second break works fine, as in when you start the game, there is 30 seconds before one of the 10 random events occur. However after you complete this task, the game jumps straight into another task without any breaks.

Thanks in advance.

Here is the script, by the way:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class Headache : MonoBehaviour {
    public bool headache = false;
    public bool gameOver = false;
    public bool waitTime = false;
    public bool completeTask = true;
    public bool startTask = false;

    public float headacheDegree = 0;
    public float randomEvent;

    public GUIStyle black;

    public GameObject FPSPlayer;

    public GameObject kid1;
    public GameObject kid2;
    public GameObject kid3;
    public GameObject kid4;
    public GameObject kid5;
    public GameObject kid6;



    GameObject hitObject;

    public Texture2D gameOverTexture;

    GUIStyle centerText;

    void Start () {

	}
	
	IEnumerator headacheUpdate()
    {
        waitTime = true;
        headacheDegree++;
        yield return new WaitForSeconds(0.5f);
        waitTime = false;
    }
	void Update () {
        if(kid1.GetComponent<armWavingAnimation>().isCry == true)
        {
            headache = true;
        }

        if (kid2.GetComponent<armWavingAnimation>().isCry == true)
        {
            headache = true;
        }

        if (kid3.GetComponent<armWavingAnimation>().isCry == true)
        {
            headache = true;
        }

        if (kid4.GetComponent<armWavingAnimation>().isCry == true)
        {
            headache = true;
        }

        if (kid5.GetComponent<armWavingAnimation>().isCry == true)
        {
            headache = true;
        }

        if (kid6.GetComponent<armWavingAnimation>().isCry == true)
        {
            headache = true;
        }

        if (kid6.GetComponent<armWavingAnimation>().isCry == false)
        {
            if (kid5.GetComponent<armWavingAnimation>().isCry == false)
            {
                if (kid4.GetComponent<armWavingAnimation>().isCry == false)
                {
                    if (kid3.GetComponent<armWavingAnimation>().isCry == false)
                    {
                        if (kid2.GetComponent<armWavingAnimation>().isCry == false)
                        {
                            if (kid1.GetComponent<armWavingAnimation>().isCry == false)
                            {
                                headache = false;
                            }
                        }
                    }
                }
            }
        }

        if (headache)
        {
            if(waitTime == false)
            StartCoroutine(headacheUpdate());
        }
            if (headacheDegree >= 100)
        {
            gameOver = true;
        }

        if (headacheDegree < 0)
        {
            headacheDegree = 0;
        }

        if (Input.GetMouseButtonDown(0))
        {

            RaycastHit hit;

            Ray xray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(xray, out hit, 5))
                if (hit.rigidbody)
                {
                    hitObject = hit.collider.gameObject;
                    if (hitObject.tag == "Pain")
                    {
                        print("checkpoint");
                        headacheDegree -= 50;
                        Object.Destroy(hitObject, 0.1f);
                    }
                }
        }
        if (completeTask)
        {
            kid1.GetComponent<armWavingAnimation>().isCry = false;
            kid2.GetComponent<armWavingAnimation>().isCry = false;
            kid3.GetComponent<armWavingAnimation>().isCry = false;
            kid4.GetComponent<armWavingAnimation>().isCry = false;
            kid5.GetComponent<armWavingAnimation>().isCry = false;
            kid6.GetComponent<armWavingAnimation>().isCry = false;
            randomEvent = Mathf.Round(Random.Range(1.0f, 10.0f));
            StartCoroutine(randomEventWait());
        }

        if (startTask)
        {
            if (randomEvent == 1)
            {
                kid1.GetComponent<armWavingAnimation>().isCry = true;
                kid2.GetComponent<armWavingAnimation>().isCry = true;
                kid3.GetComponent<armWavingAnimation>().isCry = true;
            }

            if (randomEvent == 2)
            {
                kid4.GetComponent<armWavingAnimation>().isCry = true;
            }

            if (randomEvent == 3)
            {

            }

            if (randomEvent == 4)
            {

            }

            if (randomEvent == 5)
            {

            }

            if (randomEvent == 6)
            {

            }

            if (randomEvent == 7)
            {

            }

            if (randomEvent == 8)
            {

            }

            if (randomEvent == 9)
            {

            }


            if (randomEvent == 10)
            {

            }
        }

    }

    IEnumerator randomEventWait()
    {
        yield return new WaitForSeconds(30);
        completeTask = false;
        startTask = true;
    }

    void OnGUI()
    {
        if (gameOver == false)
        {
            GUI.contentColor = Color.black;
            if (startTask)
            {
                GUI.Label(new Rect(40, 40, 200, 100), "<size=30>" + headacheDegree.ToString() + "</size>", black);
                if (randomEvent == 1)
                {
                    GUI.Label(new Rect(40, 100, 350, 100), "<size=30>Go find the Golden Block and put it on the table.</size>", black);
                }

                if (randomEvent == 2)
                {
                    GUI.Label(new Rect(40, 100, 350, 100), "<size=30>The child crying is Hungry. Get their lunchbox.</size>", black);
                }

                if (randomEvent == 3)
                {

                }

                if (randomEvent == 4)
                {

                }

                if (randomEvent == 5)
                {

                }

                if (randomEvent == 6)
                {

                }

                if (randomEvent == 7)
                {

                }

                if (randomEvent == 8)
                {

                }

                if (randomEvent == 9)
                {

                }


                if (randomEvent == 10)
                {

                }
            }
        }

        if (gameOver)
        {
            FPSPlayer.GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = false;

            GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), gameOverTexture, ScaleMode.ScaleAndCrop, true, 10.0F);

            string firstLine = "<size=50>You Died due to an Overwhelming Headache!</size>";
            string secondLine = "<size=50>Press 'Return' to Restart!</size>";

            float textWidth = firstLine.Length * 20;
            float textWidth2 = secondLine.Length * 15;

            GUI.Label(new Rect(Screen.width / 2 - textWidth / 2, Screen.height / 2 - 40, textWidth, 75), firstLine);
            GUI.Label(new Rect(Screen.width / 2 - textWidth2 / 2, Screen.height / 2 + 40, textWidth2, 75), secondLine);

            if (Input.GetKeyDown(KeyCode.Return))
            {
                SceneManager.LoadScene(0);
            }
        }
    }
}

Seems you are not setting startTask = false after starting the task. This means that after your 30 second wait coroutine, startTask is already true after the first time so will immediately start the next task.