IEnumerator PauseHandler() { bool paused = false; StringEventListener listener = null; GameObject panelGo = null; string pauseAction = ""; while (!winnerDeclared) { bool startPause = false; bool stopPause = false; bool concede = false; // handle escape key or menu click if (Input.GetKeyUp(KeyCode.Escape) || wantPause) { wantPause = false; if (paused) { stopPause = true; } else { startPause = true; } } // handle event listener response if (pauseAction == "continue") { stopPause = true; } else if (pauseAction == "concede") { stopPause = true; concede = true; } // handle start pause if (startPause) { // instantiate pause modal panelGo = Instantiate(pausePanelPrefab, GetCanvas().gameObject.transform); yield return(null); // wait a frame for panel initialization // add listener to panel pauseAction = ""; listener = panelGo.AddComponent <StringEventListener>(); listener.SetEvent(pauseActionSelected); listener.Response.AddListener((msg) => { pauseAction = msg; }); // actually pause Time.timeScale = 0f; paused = true; } // handle stop pause if (stopPause) { pauseAction = ""; // tear down modal if (panelGo != null) { Destroy(panelGo); } // continue Time.timeScale = 1f; paused = false; } // handle concede action if (concede) { pauseAction = ""; // declaring a winner should cause pause manager to finish DeclareWinner(spawnedEnemies[0]); } // wait til next frame yield return(null); } }