• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jjdoughboy · Aug 28, 2020 at 09:29 PM · buttonpause menuwavesretry

How do fix my pause menu

I have a tower defense game I'm working on and have 4 levels with a pause menu that can toggled and within the pause menu I have a retry button that is supposed to reload the level scene and restart the level. the level starts with a enemy wave countdown before the enemy spawns, when the retry button is pressed it will reload the scene but the 1st wave countdown of the level after the retry button was pressed it won't start. I'm not to sure why that is case as all other player interactions work properly, its just the wave countdown that doesn't start.

This is my pause menu script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class PauseMenu : MonoBehaviour
 {
     public GameObject pauseUI;
 
     public GameObject playerPanelUI;
 
     public GameObject shopPanelUI;
 
     public string mainMenu = "MainMenu";
 
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.P))
         {
             Toggle();
         }
     }
 
     public void Toggle()
     {
         pauseUI.SetActive(!pauseUI.activeSelf);
 
         if(pauseUI.activeSelf)
         {
             Time.timeScale = 0f;
 
             playerPanelUI.SetActive(false);
             shopPanelUI.SetActive(false);
         }
         else
         {
             Time.timeScale = 1f;
 
             playerPanelUI.SetActive(true);
             shopPanelUI.SetActive(true);
         }
     }
 
     public void Retry()
     {
         Toggle();
 
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 
     public void Menu()
     {
         SceneManager.LoadScene(mainMenu);
     }
 }


This my Wave spawner script my wave countdown is attached to this script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 
 public class WaveSpawner : MonoBehaviour
 {
     public Transform enemyPrefab;
     public Transform spawnPoint;
     public float waveInterval = 5f;
     public float countdown = 2f;
 
     private int waveIndex = 0;
 
     public Text waveCountdown;
 
     public Wave[] waves;
 
     public GameManager gameManager;
 
     [Header("How Many Enemies Are Left")]
     public static int EnemiesAlive;
 
     void Update()
     {
         if(EnemiesAlive > 0)
         {
             return;
         }
 
         if (countdown <= 0f)
         {
             // The coroutine will pause execution and automatically
             // resume at the next frame
             // With coroutines we begin to model behavior without affecting performance
             StartCoroutine(SpawnWave());
             Debug.Log("Wave Start");
             countdown = waveInterval;
             return;
         }
 
         if (waveIndex == waves.Length)
         {
             if (EnemiesAlive < 1)
             {
                 this.enabled = false;
                 gameManager.WinLevel();
             }
         }
 
         countdown -= Time.deltaTime;
 
         waveCountdown.text = string.Format("{0:00.00}", countdown);
 
         countdown = Mathf.Clamp(countdown, 0f, Mathf.Infinity);
     }
 
     /// <summary>
     // IENumerator allows for this function to pause before being execute
     // in this case we want out waves to spawn incrementally
     // On top of this function running incrementally
     /// </summary>
     /// <returns></returns>
     IEnumerator SpawnWave()
     {
         PlayerStats.Waves++;
       
 
         Wave wave = waves[waveIndex];
         for (int i = 0; i < wave.count; i++)
         {
 
             SpawnEnemy(wave.enemy);
             // the amount of time the function will pause given the IENumberator
             yield return new WaitForSeconds(0.5f);
         }
 
         waveIndex++;
     }
 
     void SpawnEnemy(GameObject enemy)
     {
         Instantiate(enemy, spawnPoint.transform.position, spawnPoint.transform.rotation);
 
         EnemiesAlive++;
        
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ShySam · Aug 29, 2020 at 12:02 AM

did you try to add an if statement to the count down before it start or when it restarts. insted of having just [ if (countdown <= 0f) ] you can add one more rule simple to it like:

 //add this to your countdown script <WaveSpawner >
 public bool Start_CountDown;
 
 //and add that rule to if (countdown <= 0f) ... so it will be like:
 if (countdown <= 0f && Start_CountDown == true ) 
 
 //and add this to your enemies secripts
 public GameObject Wave_Spawner_Manager;  //The object that has WaveSpawner script
 
 void Awake () {
 Wave_Spawner_Manager.GetComponent<Wave_Spawner_Manager>().Start_CountDown = ture;
 }
 
 

tell me if it worked :D

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

164 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game stays paused after Restart. 1 Answer

my wave spawner not working properly 1 Answer

[SOLVED] How can I continue game on UI button press? 1 Answer

GUI Buttons not working after scene transition 0 Answers

Unity 5 - pause menu bug 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges