- Home /
Pause Game Through Button UI Click Instead of Escape Key?
So here is my gamemanager script. I feel like this is a simple fix but I can't seem to remember the correct method to have the pause done via button instead of keypress.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
 public class GameManager : MonoBehaviour
 {
     private const int COIN_SCORE_AMOUNT = 5;
     private const int OBSTACLE_DODGE_AMOUNT = 10;
     public static GameManager Instance { set; get; }
 
     private bool isGameStarted = false;
     private PlayerMotor motor;
 
     // UI and UI Fields
     public Text scoreText, coinText, modifierText, hiscoreText, timeText;
     public float score, coinScore, modifierScore, timeScore;
     private int lastScore;
     public int difficultyLevel = 0;
     public double TimeLevel = 0.04;
     private int maxDifficultyLevel = 20;
     private int scoreToNextLevel = 10;
     private bool isDead = false;
     public GameObject flash;
     public static bool GameIsPaused = false;
     public GameObject pauseMenuUI;
     public Button PauseButton;
 
     // Death Menu
     public Animator deathMenuAnim;
     public Text deadscoreText, deadcoinText;
 
     private void Awake()
     {
         Instance = this;
         modifierScore = 1;
         motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor>();
         flash = GameObject.Find("Tap Anywhere to Start");
         PauseButton = GetComponent<Button>();
 
         modifierText.text = "x" + modifierScore.ToString("0.0");
         coinText.text = coinScore.ToString("0");
         scoreText.text = scoreText.text = score.ToString("0");
 
         hiscoreText.text = PlayerPrefs.GetInt("Hiscore").ToString();
     }
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             if (GameIsPaused)
             {
                 Resume();
             }
             else
             {
                 Pause();
             }
         }
 
         if (MobileInput.Instance.Tap && !isGameStarted)
         {
             isGameStarted = true;
             motor.StartRunning();
             Time.timeScale = 1f;
             flash.SetActive(false);
         }
 
         if(isGameStarted)
         {
             // Bump the Score Up
             score += (Time.deltaTime * modifierScore);
             if (lastScore != (int)score)
             {
             lastScore = (int)score;
                 scoreText.text = score.ToString("0");
             }
         }
         {
 
             if (isDead)
                 return;
 
             if (score >= scoreToNextLevel)
                 LevelUp();
 
             score += Time.deltaTime * difficultyLevel;
             scoreText.text = ((int)score).ToString();
         }
         void LevelUp()
         {
             if (difficultyLevel == maxDifficultyLevel)
                 return;
 
             scoreToNextLevel *= 2;
             difficultyLevel++;
             modifierScore += 1;
 
             Time.timeScale += 0.04f;
             timeScore += 0.04f;
         }
     }
 
     public void GetCoin()
     {
         coinScore++;
         coinText.text = coinScore.ToString("0");
         score += COIN_SCORE_AMOUNT;
         scoreText.text = scoreText.text = score.ToString("0");
     }
 
     public void DodgeObstacle()
     {
         score += OBSTACLE_DODGE_AMOUNT;
         scoreText.text = scoreText.text = score.ToString("0");
     }
 
     public void UpdateModifier(float modifierAmount)
     {
         modifierScore = 1.0f + modifierAmount;
         modifierText.text = "x" + modifierScore.ToString("0.0");
     }
 
     public void OnPlayButton()
     {
         UnityEngine.SceneManagement.SceneManager.LoadScene("Hallway Dash");
     }
 
     public void OnMenuButton()
     {
         UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
     }
 
     public void OnDeath()
     {
         FindObjectOfType<AudioManager>().Play("Death");
         isDead = true;
         deadscoreText.text = score.ToString("0");
         deadcoinText.text = coinScore.ToString("0");
         deathMenuAnim.SetTrigger("Dead");
         modifierScore = 0f;
 
         if(score > PlayerPrefs.GetInt("Hiscore"))
         {
             float s = score;
             if (s % 1 == 0)
                 s += 1;
             PlayerPrefs.SetInt("Hiscore", (int)score);
         }
     }
 
         public void Resume()
         {
             pauseMenuUI.SetActive(false);
             Time.timeScale = timeScore;
             GameIsPaused = false;
         }
 
         public void Pause()
         {
             pauseMenuUI.SetActive(true);
             Time.timeScale = 0f;
             GameIsPaused = true;
     }
 }
I'm not sure if I understand your question, can't you just assign the Pause() function to the button?
Answer by SolidSlish · Nov 23, 2019 at 09:10 PM
You mean write a new script, add to the two public voids on the button and then attach it?
That won't work, the button will pause and unpause at the sametime....
$$anonymous$$aybe this will work:
  private void Awake()
      {
          Instance = this;
          modifierScore = 1;
          motor = GameObject.FindGameObjectWithTag("Player").GetComponent<Player$$anonymous$$otor>();
          flash = GameObject.Find("Tap Anywhere to Start");
          PauseButton = GetComponent<Button>();
 
          PauseButton.onClick.AddListener(() => ButtonPause());
 
          modifierText.text = "x" + modifierScore.ToString("0.0");
          coinText.text = coinScore.ToString("0");
          scoreText.text = scoreText.text = score.ToString("0");
  
          hiscoreText.text = PlayerPrefs.GetInt("Hiscore").ToString();
      }
 
     void ButtonPause()
 {
              pause$$anonymous$$enuUI.SetActive(true);
              Time.timeScale = 0f;
              GameIsPaused = true;
 
 PauseButton.onClick.AddListener(() => ButtonUnpause());
 PauseButton.onClick.RemoveListener(() => ButtonPause());
 
      }
  void ButtonUnpause()
          {
              pause$$anonymous$$enuUI.SetActive(false);
              Time.timeScale = timeScore;
              GameIsPaused = false;
 
 PauseButton.onClick.AddListener(() => ButtonPause());
 PauseButton.onClick.RemoveListener(() => ButtonUnpause());
 
          }
 
Do I make that a new script or add that on my current gamemanager one?
Okay, it didnt work and now for some reason, my gamemanager deactivates on startup
Answer by BloobGames · Nov 24, 2019 at 04:21 PM
This should work:
 bool isPaused;
 
     public void Pause()
     {
         if(!isPaused)
         {
             Time.timeScale = 0;
             isPaused = true;
         }
         else
         {
             Time.timeScale = 1;
             isPaused == false;
         }
     }
If it did, could u make this answer the top answer?
That wasn't what I asked for at all, and an improved version of that code is already in my script, sorry.
Your answer
 
 
             Follow this Question
Related Questions
Game stays paused after Restart. 1 Answer
GUI Buttons not working after scene transition 0 Answers
Buttons will not change color 11 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                