Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 SolidSlish · Nov 23, 2019 at 07:58 PM · buttonpausekeyboardpause menupause game

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;
     }
 }
Comment
Add comment · Show 1
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
avatar image HenriMR · Nov 23, 2019 at 09:08 PM 0
Share

I'm not sure if I understand your question, can't you just assign the Pause() function to the button?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 5 · 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
avatar image HenriMR · Nov 23, 2019 at 09:24 PM 0
Share

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());
 
          }
 

avatar image SolidSlish HenriMR · Nov 23, 2019 at 09:29 PM 0
Share

Do I make that a new script or add that on my current gamemanager one?

avatar image SolidSlish HenriMR · Nov 23, 2019 at 09:55 PM 0
Share

Okay, it didnt work and now for some reason, my gamemanager deactivates on startup

avatar image HenriMR SolidSlish · Nov 23, 2019 at 11:12 PM 0
Share

Is it returning a error massage?

Show more comments
avatar image
0

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?

Comment
Add comment · Show 1 · 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
avatar image SolidSlish · Nov 24, 2019 at 04:38 PM 0
Share

That wasn't what I asked for at all, and an improved version of that code is already in my script, sorry.

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.

Follow this Question

Answers Answers and Comments

140 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

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

How to make a pause menu with Gui Texture?? 2 Answers

how to pause the mouselook script during pause? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges