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 kit20 · Jun 07, 2016 at 09:56 AM · buttontoggleif statementpause menuresume

Resume Button / Pause Menu odd interaction

This has been driving me crazy for 3 hours, I hope you guys can help me. It's not easy being a decent artist but a newbie coder. Unity certainly narrows the gap a lot but at some point, one still runs into tougher issues.

I have a simple Pause Menu GameObject with 3 UI buttons that toggles ON or OFF when you press the Escape key. Works fine, no issues here. All of this happens within the Update function (with the exception of the variable definitions).

 // Toggle Pause Menu ON or OFF
         if (Input.GetButtonDown("Cancel"))
         {
             if (!isPaused)
             {
                 isPaused = true;
                 pauseMenu.SetActive(true);
                 Time.timeScale = 0;
             }
             else
             {
                 isPaused = false;
                 pauseMenu.SetActive(false);
                 Time.timeScale = 1;
             }
         }

One of these 3 buttons is a Resume button that basically fulfills the same role as unpausing with the Escape key after it's already paused. But when I click it, everything works fine with one exception: I have to press the Escape key twice to pause the game again. It will not recognize the first key press.

The button itself is a UI Button with a script attached that executes an On Click () function. And here's the function itself from the script:

 // Resume (Go back to game screen)
     public void OnResume()
     {
         isPaused = false;
         pauseMenu.SetActive(false);
         Time.timeScale = 1;
     }

I know that everything within the Update function is called every frame and I know the OnResume() function is only called when I click the UI Button. So what am I missing?

I assume the OnResume() function doesn't set the isPaused boolean variable to false (even though I clearly added the line) which is why the first time I press Escape, nothing seems to happen (but the if statement in the Update function probably sets it to false) and then the second time it happens because it sets it to true.

Thank you so much for reading. I look forward to your responses. : )

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by kit20 · Jun 09, 2016 at 12:03 PM

@ColdJackle - You were right about the variable not being synced properly because of lack of communication between the methods. Apparently the solution was very simple: define the isPasued variable as static.

 static bool isPaused = false;

That took care of everything. Don't even need to initialize anything in Start () now. : )

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
1

Answer by ColdJackle · Jun 07, 2016 at 11:38 AM

Is the "isPaused" variable in the button script the same as the one in the menu script? Because it sounds like you have 2 scripts and 2 variables, but you didn't provide the part when theses two variables are synced. The Menu-Script has to be notified about the change. I would recommend the following:

  1. Delete the Script on your UI Button

  2. Create this method in the main script:

    public void TogglePause() { isPaused = !isPaused; pauseMenu.SetActive(isPaused); Time.timeScale = isPaused ? 0 : 1; }

  3. Add this line to the "Start" method of the main script (to be sure^^)

    isPaused = true; TogglePause();

  4. Select the UI-Button in the GameObject-List of your current scene and open the Inspector tab

  5. Scroll down to the Button-Script and click the little plus-icon on the "OnClick()"-event panel (at the bottom)

  6. Drag the Gameobject with the menu-script on the empty object field in the new event item and select the "TogglePause"-method from the class->method list.

  7. EDIT: Oh and Replace all the code you wrote in that if-clause with a call to the TogglePause()-method, like this:

    if (Input.GetButtonDown("cancel")) { TogglePause(); }

This is like calling the method from OnClick, but with less coding. Also you can call functions with one primitve parameter (int, float, bool, etc.), that you can set, or an UI-Object, like the clicked button.

Hope this helps :)

Comment
Add comment · Show 7 · 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 kit20 · Jun 07, 2016 at 03:35 PM 0
Share

@ColdJackle - Thank you very much for your reply. But now in addition to having to press the Escape key twice to get the pause menu to come up again... I also have to click Resume twice to get back to the game. I recreated this specific section in a brand new scene but I have the same issue. This happens both in the Editor and in a Standalone build. Here's the full script:

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ainScript : $$anonymous$$onoBehaviour
 {
 
     private bool isPaused = false;
     public GameObject pause$$anonymous$$enu;
 
     // Use this for initialization
     void Start ()
     {
         isPaused = false;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if (Input.GetButtonDown("Cancel"))
         {
             TogglePause();
         }
     }
     
     public void TogglePause()
     {
         isPaused = !isPaused;
         pause$$anonymous$$enu.SetActive(isPaused);
         Time.timeScale = isPaused ? 0 : 1;
     }
     
     public void OnQuit()
     {
         #if UNITY_STANDALONE
             Application.Quit();
         #endif
     
         #if UNITY_EDITOR
             UnityEditor.EditorApplication.isPlaying = false;
         #endif
     }
 }

It's attached to the Canvas since I'm never gonna destroy or disable that object as well as the Resume and Quit buttons, each calling its specific method when clicked.

That's all there is in the scene but if you want to take a direct look at it, I can pack it into a zip file and upload it to my Dropbox.

avatar image ColdJackle kit20 · Jun 07, 2016 at 04:10 PM 1
Share

Ah, I see a wrong step in my reply: Please switch "isPaused = false" in "Start()" to "isPaused = true; TogglePause();". This way the everthing gets set fine. If you still encounter any double presses, then there is something wrong with Unity, because I recreated the scene and except for the missing code in Start, everything is working for me.

avatar image kit20 ColdJackle · Jun 07, 2016 at 04:28 PM 0
Share

I'm running the Unity version 5.3.5f1 Personal Edition, if that helps.

Sadly, I still can't get it to work flawlessly. Now I have to press the Escape key 5 times before the menu comes up the first time. Then double presses to toggle it on and off again as well as double press to Resume. Which makes no sense at all to me.

But thanks for taking an interest in my issue, I really appreciate any bit of help I can get.

Show more comments
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why doesnt my resume button work 0 Answers

I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers

Time.timeScale doesnt work with GUI? 0 Answers

Audio problems with unity after reloading my game from the main menu 1 Answer

Pause-Resume one button using switch statements? 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