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 /
  • Help Room /
avatar image
2
Question by Chappy · Apr 29, 2013 at 08:19 PM · c#pause

How to program a pause/unpause button in C#.

I'm really new to programming (been at it on and off for about 3 or so weeks) and I'm currently try to program a pause and unpause button for a game. I've looked at a few video's but I'm mostly trying to figure this all out myself and avoid just copying code down so I've mostly written this thing myself. I've been through a couple iterations of this code, all of them allowed me to pause but not unpause. This most recent one allows me to pause sometimes and unpause sometimes. It's very weird, I can tap the pause button and an object stops for a second but if I press it a lot (or simply old the button down) it'll stop completely. It works the exact same way for unapausing, tapping causes it to start for a second while tapping a lot will cause it to start again.

Again, I have no real Idea what I'm doing so you'll have to excuse the most likely mediocre code.

 using UnityEngine;
 using System.Collections;
 
 public class PauseGame : MonoBehaviour {
 
     bool Pause = false;
     
     void Update()
     {
         
     if (Pause == false)
         {
             Time.timeScale = 1;
         }
         
     else 
         {
             Time.timeScale = 0;
         }
         
         
     if (Input.GetKey(KeyCode.P))
         {
             if (Pause == true)
             {
                 Pause = false;
             }
             
         else
             {
                 Pause = true;
             }
         }
      
     
     }
 
     
 }
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
12
Best Answer

Answer by hoy_smallfry · Apr 29, 2013 at 08:27 PM

GetKey will trigger every frame that the key is held down. That means that for however many frames you've been holding it down, your Pause variable is alternating between true and false.

Take this an example: Pause starts out false. You've held it down for more than one frame. Lines 24 though 32 of your code will dictate that after the first frame, Pause gets set as true. The second frame, it gets set as false, the third frame creates the same condition as the first frame, again and again.

If you only want the input to trigger the pause the first time it detects the key is down, use Input.GetKeyDown.

You could probably do away with the Pause value as well and simply change the time scale when the button is pressed, like so:

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.P))
     {
         if (Time.timeScale == 1)
         {
             Time.timeScale = 0;
         }
         else
         {
             Time.timeScale = 1;
         }
     }
 }
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 Chappy · Apr 29, 2013 at 09:00 PM 0
Share

Wow, I can't believe it was that simple, I was really over-complicating it apparently. I think I had tried Get$$anonymous$$eyDown before but something messed up and I went back to Get$$anonymous$$ey. Thanks.

avatar image
1

Answer by Numonic · Dec 26, 2017 at 07:50 PM

This was simple and concise especially with me using my Logitech Controller on PC. Thanks!

 public void   PauseGameFeature()
     {
         if (Input.GetKeyUp(KeyCode.Joystick1Button9))
         {
             if(Time.timeScale == 1)
             {
                 Pause();
             }
             else
             {
                 UnPause();
             }  
         }
     }
 
     public void Pause()
     {
         Time.timeScale = 0;
         pauseGame.Play();
         level1Music.Stop();
         playerController.enabled = false;
         print("We are pressing StartButton and Pausing game.");
 
 
     }
     public void UnPause()
     {
         Time.timeScale = 1;
         pauseGame.Play();
         level1Music.Play();
         playerController.enabled = true;
         print("We are pressing StartButton and UnPausing 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

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

17 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

Related Questions

PAUSE MENU - - If i press "w" and "space change scene.. 0 Answers

Help with pause menu? 0 Answers

When Resuming game after losing focus I just get a black screen? 0 Answers

How to make all objects pause in the game when i press pause button ? 1 Answer

Make my moving platforms pause at its endpoints using c# 2 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