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 /
avatar image
0
Question by MrAwesome343 · May 16, 2016 at 01:46 PM · booleantimescalepause gamepausegame

Making a pause menu resume

So I'm trying to make a pause feature in my game, and that works perfectly. Unfortunately, the game stays paused infinitely, which isn't so perfect. The way I have it programmed seems like it should work, at least to me, but it doesn't. Any help? Here's the code:

 void FixedUpdate()
     {
         if (Input.GetKeyDown("escape"))
         {
             paused = !paused;
         }
 
         if (paused == true)
         {
             Time.timeScale = 0;
         }
 
         if (paused == false)
         {
             Time.timeScale = 1;
         }
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 Flaring-Afro · May 16, 2016 at 08:17 PM 0
Share

You should only use FixedUpdate for physics. It can come back to haunt you if you don't. Use coroutines, or deltatime in update, for anything time based.

Also, line 13 should just read "else". A boolean can only be two things, so checking if it's the other is completely unnecessary and slows down the game (although admittedly it would take a ton of if statements to be noticeable).

2 Replies

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

Answer by flashframe · May 16, 2016 at 04:28 PM

It's because you're using FixedUpdate which isn't called when timeScale is set to zero. http://docs.unity3d.com/ScriptReference/Time-timeScale.html

Try changing it to Update(). That should work.

Comment
Add comment · Show 3 · 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 MrAwesome343 · May 16, 2016 at 07:11 PM 0
Share

Thank you! It works perfect!

avatar image aballif · May 16, 2016 at 09:13 PM 0
Share

How would you make it so you could have a UI button ins$$anonymous$$d of pressing escape? Is there a OnButtonPress function or something like it?

avatar image flashframe aballif · May 16, 2016 at 09:47 PM 0
Share

@aballif If you are using a Unity UI Button component, then you can set an "OnClick" Event in the inspector. You can drag your custom script in there and assign the method you want to run.

avatar image
0

Answer by vittu1994 · May 16, 2016 at 04:21 PM

can you toggle between each boolean with that first if-statement? In that case you should write the two others like this:

 if(paused)
 {
     Time.timeScale = 0;
 }
 else if(!paused)
 {
     Time.timeScale = 1;
 }

paused is paused = true and !paused = paused = false.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game doesn't get paused in Release build 0 Answers

Pausing an Invoke for a time 2 Answers

Question on timeScale and boolean script. 0 Answers

Unpause game when the timer reaches 0 1 Answer

Confusion on simple pause screen 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