Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by AlexTheHollow · Nov 19, 2015 at 02:53 AM · c#unity 5pause menu

Help with Pause Menu

I'm trying to get my pause menu to open but it won't.

I followed this tutorial and it seems to work for him but not for me. https://www.youtube.com/watch?v=xIevsYimJYc https://www.youtube.com/watch?v=TatAnGj1RMg

I can't get the script to appear in the functions when try to assign them in a canvas' buttons either. I don't know if that would be why or not.

 using UnityEngine;
 using System.Collections;
 
 public class PauseMenu : MonoBehaviour {
 
     public GameObject PauseUI;
     private bool paused = false;
 
 
     void Start() 
     {
         PauseUI.SetActive (false);
     }
 
     void Update() 
     {
         if(Input.GetButtonDown("escape"))
            {
             paused = !paused;
            }    
 
     if(paused) 
     {
     PauseUI.SetActive(true);
     Time.timeScale = 0;
     }
 
     if(!paused) 
     {
     PauseUI.SetActive(false);
     Time.timeScale =  1;
     }
 }
 
     public void Resume() 
     {
     paused = false;
     }
     
     public void MainMenu() 
     {
     Application.LoadLevel(1);
     }
 
     public void Quit() 
     {
     Application.Quit();
     }
 }
 
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 Dibbie · Nov 19, 2015 at 09:24 AM

Try if(paused == false) instead of if(!paused), I ran into this problem with my pause screen too, I have ZERO idea why it matters to Unity, but it seemd to somehow register paused == false verse paused not equal to itself... Again, no idea why.

Comment
Add comment · Show 6 · 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 AlexTheHollow · Nov 19, 2015 at 02:56 PM 0
Share

Still nothing.

I tried updating the code like this by messing with the paused = !paused to what you said and adding in another if(Input.GetButtonDown("escape")) but changed the Button to $$anonymous$$ey. Still nothing.

It's still not appearing in the canvas/buttons functions.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Pause$$anonymous$$enu : $$anonymous$$onoBehaviour {
 
     public GameObject PauseUI;
     private bool paused = false;
 
 
     void Start() 
     {
         PauseUI.SetActive (false);
     }
 
     void Update() 
     {
         if (Input.Get$$anonymous$$eyDown ("escape")) {
             paused = true;
         }    
 
         if (paused) {
             PauseUI.SetActive (true);
             Time.timeScale = 0;
         }
 
         if (paused == false) {
             if (Input.Get$$anonymous$$eyDown ("escape")) {
                 PauseUI.SetActive (false);
                 Time.timeScale = 1;
             }
         }
     }
     public void Resume() 
     {
     paused = false;
     }
     
     public void $$anonymous$$ain$$anonymous$$enu() 
     {
     Application.LoadLevel(1);
     }
 
     public void Quit() 
     {
     Application.Quit();
     }
 }
 
avatar image Dibbie · Nov 19, 2015 at 03:56 PM 0
Share

Hmm, try checking the Layer as well, and make sure your Pause UI is a higher number then your others, so it shows up first.

Also, for "escape", try changing the escape key lines to a keycode:

if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Escape))

avatar image AlexTheHollow · Nov 19, 2015 at 04:43 PM 0
Share

Nope... Still nothing...

avatar image Dibbie AlexTheHollow · Nov 19, 2015 at 04:59 PM 0
Share

Thats weird, I just ran your code in a new project, made a very basic Canvas with a green Panel, nice and simple

Then put the entire Canvas as the "game object"/"pause UI".

Pressing Escape, pops the menu up, and because you have "pause = true" on escape, this Canvas stays open, but your code does work fine... I also created a second Canvas with the Sort Order (not Layer, my bad), of 5 for example (higher then the "UI Canvas"), and the UI one to 0. The UI one never shows up, but the code still works, now flipping this, the UI being 5, and the other second UI I made as 0, the first one (the menu) actually appears above it. Whats happening for you trying this?

avatar image AlexTheHollow · Nov 19, 2015 at 08:37 PM 0
Share

Okay. I got it to pop up. It stays up on button press.

The solution was that I had the script on the $$anonymous$$ain Canvas/PauseUI while the buttons On Click() GameObject set to the $$anonymous$$ain Camera.

Upon setting the script to the Camera, it pops up on pressing escape.

Now, any idea how I would get it close? Would I need to go back to !pause?

Also, thank you very much for your assistance.

Edit: On further inspection, the resume button doesn't work. I'm not sure about the quit button because I've only built it into the Unity Engine. It should work fine, though.

On a side note, the $$anonymous$$ain $$anonymous$$enu button works but when I click play after going back to the menu (through clicking in the pause menu), it loads the first level and the music but neither the characters nor the enemies move.

avatar image Dibbie AlexTheHollow · Nov 19, 2015 at 08:53 PM 0
Share

You COULD use pause = !pause again every time they press the key, you could also do a logical switch if you want, like, when they press escape:

 if(time.timeScale == 0){
 //we know the games paused, so unpause it
 pause = false;
 }
 else{
 //We know the game has to be unpaused, so pause it
 pause = true;
 }

If your button is set up correctly, the OnClick() event, is referencing the object that has the script on it, then there should be no problems, you can test it with a print("exiting game..."); line or something alike, so you can check your console when you press it, just to make sure it IS actually running, cause then you know it would actually exit later on in the build.

Same thing for resume, make sure its on the button, for resume, you want to do more then just set pause to false, you also want to reset the timeScale, OR take the code of the key press outside your false pause check in your Update, because what your update will do is, if paused is false, which lets assume it is... ONLY WHEN THEY PRESS Escape during that time pause is still false, then resume the game... But if paused is true, your other if statement will run right after... So it will remain paused again.

And the movement thing could be due to timeScale, try just resetting it before loading the level again. time.timeScale = 1;

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

Creating a pause screen and resuming gameplay? 2 Answers

Cursor does not show up in pause menu. C#,Cursor isn't showing up in Pause Menu when game is built. C# 0 Answers

Script attached to An enemy but dosent work right if there are multiple enemies. 2 Answers

Is there a Scanner/Digging system in unity???? 1 Answer

Make object fall down and start again from top. 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