IEnumerator LevelFinished() { int levelFinished = 0; BaseHealth bh = FindObjectOfType <BaseHealth>(); int healthDeduction = bh.startHealth - bh.GetHealth(); int lastSpawnCount = 0; while (healthDeduction + enemiesKilled != totalEnemiesToSpawn && levelFinished != 6) { if (lastSpawnCount != healthDeduction + enemiesKilled) { lastSpawnCount = healthDeduction + enemiesKilled; levelFinished = 0; } else { // Level was not ending on a rare occasion levelFinished++; Debug.Log(levelFinished); } healthDeduction = bh.startHealth - bh.GetHealth(); yield return(new WaitForSeconds(1)); } AudioSource.PlayClipAtPoint(victorySfx, Camera.main.transform.position); for (int i = 5; i > 0; i--) { startDelay.text = "Returning\n to level select \n in " + i + " seconds"; startDelay.fontSize = 32; yield return(new WaitForSeconds(1)); } SceneManager.LoadScene("LevelSelect"); }