Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 AzureKnight45 · May 07, 2014 at 02:18 AM · c#pauseresume

GUI resume button not working?

I've made a simple method of pausing my game within my GameManager class as seen here:

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
 
     private string[] levelList; //Create a list of levels
 
     public int playerScore;
 
     private bool isPaused;
 
     void Awake() {
         DontDestroyOnLoad(this.gameObject);
     }
 
     void Start () {
         isPaused = false;
         Screen.lockCursor = true;
     }
     
     void Update () {
         if (Input.GetKeyDown(KeyCode.Escape)) {
             pauseToggle();
         }
     }
 
     void OnGUI() {
         if (isPaused) {
             GUI.Box(new Rect(-8, -8, Screen.width + 8, Screen.height + 8), "");
 
             GUI.Box(new Rect(Screen.width / 2 - 160, Screen.height / 2 - 240, 320, 480), "Pause Menu");
 
             if (GUI.Button(new Rect(Screen.width / 2 - 120, Screen.height / 2 - 200, 120, 80), "Close Menu")) {
                 isPaused = false;
                 Time.timeScale = 1.0f;
                 Screen.lockCursor = true;
                 Debug.Log("Unpaused");
                 isPaused = false;
             }
 
             if (GUI.Button(new Rect(20, 70, 80, 20), "Title Screen")) {
                 Application.LoadLevel(1);
             }
         }
     }
 
     void pauseToggle() {
         if (!isPaused) {
             Time.timeScale = 0.0f;
             Screen.lockCursor = false;
             isPaused = true;
         }
         else {
             Time.timeScale = 1.0f;
             Screen.lockCursor = true;
             isPaused = false;
         }   
     }
 }

The problem is that the Escape key pauses/unpauses the game perfectly, however when the pause Resume button is clicked the GUI seems to freeze yet I can still toggle the pause state with Escape to get out of it.

I've tried both what is current under the Buttons if statement and simply using the pauseToggle function I made earlier. I must be missing something but I can't seem to figure it out.

Comment
Add comment · Show 3
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 getyour411 · May 07, 2014 at 02:24 AM 0
Share

There is no Resume button here, did you mean "Close $$anonymous$$enu" or do you have Resume somewhere else?

avatar image AzureKnight45 · May 07, 2014 at 02:40 AM 0
Share

Yes sorry, running low on sleep. The close menu basically does what a resume button would.

avatar image AzureKnight45 · May 07, 2014 at 12:34 PM 0
Share

I think this might be some kind of bug with the GUI on my end. Changed the code to match Drico's answer and still nothing. Weird thing is I just stuck a debug statement under the second button and it never got executed. It's almost as though the GUI doesn't function when the timeScale is 0. The most annoying part is that I've seen other people do this and it works but their code is always in JS.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by VIPINSIRWANI · May 07, 2014 at 07:48 AM

Hi Once check your pauseToggle() is called on every Escape key press or print something inside pauseToggle() and one more thing you should mot use*Time.timeScale* instead of that you can use boolean values for pause or Resume Game.

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
avatar image
0

Answer by DricoJD · May 07, 2014 at 09:58 AM

I Think I know the problem

Do not do (!isPaused)as this is a toggle option, which conflicts with you two sets of isPaused in ONGUI (). If you remove the double then test, if that does not work - which I highly predict it will not work then change !isPaused to (isPaused == true);

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
avatar image
0

Answer by AzureKnight45 · May 08, 2014 at 12:14 AM

I fixed it guys, just set timeScale to some ridiculously low value like 0.0000001f. Basically paused as far as normal perception is concerned. Maybe it's a bug or not but the OnGUI function seems to mess up when timeScale is absolute zero.

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 meat5000 ♦ · May 08, 2014 at 12:54 AM 1
Share

Yes its a bit obscure but setting timeScale to 0 has be known to cause lots of problems when it comes to resu$$anonymous$$g! The problem is not limited to OnGUI either :P

Your approach is a good, common way to resolve this problem. Search on answers for Pausing Game and you'll see what I mean.

avatar image
0

Answer by Wolfy1891 · Jun 20, 2021 at 12:09 AM

Obviously, this is a late answer. However, for anyone else looking for an answer.

I had this happen with my pause script. I fixed it by going into the unity input section and found that the button it had for cancel was also Esc.

I changed that to another button (specifically to backspace, but I'm sure any other button works) and there was no longer a fight for the ESC key.

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.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can't set Time.timeScale back to 1. 2 Answers

Time.timeScale doesnt work with GUI? 0 Answers

Pause Menu problem 0 Answers


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