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 TheBrokeBacks · May 08, 2014 at 08:06 PM · timepausetimescaleresumeunpause

Pause, Unpause

Okay, so I want to be able to pause and unpause my game (not using a GUI text, but instead by just pressing P) and here is my coding so far...

 using UnityEngine;
 using System.Collections;
 
 public class Pause : MonoBehaviour {
     public bool paused = false;
     void Start ()
     {
         paused = false;
         Time.timeScale = .1f;
     }
     // Update is called once per frame
     void Update () 
     {
         if (Input.GetKeyDown ("p") && paused == false)
         {
             Time.timeScale = 0;
             paused = true;
         }
         if (Input.GetKeyDown ("p") && paused == true)
         {
             Time.timeScale = 1;
             paused = false;
         }
     }
 }


btw it's in C#. It won't pause but when I hit the P button it goes from Time.timeScale = .1f; to Time.timeScale = 1;

How do I fix that (relativley new to this and I can't find the answer anywhere, trust me.)

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
5

Answer by Graham-Dunnett · May 08, 2014 at 08:11 PM

So think about it. To begin you have paused set to false. When you press the p key, your test at line 14 will pass, so you'll set paused to true. Then, at line 19, you test if the p key is pressed (it was) and if paused is true (it is). So you'll always set paused to false if the p key is pressed. So, at line 19 do an else if.

Comment
Add comment · Show 2 · 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 Lo0NuhtiK · May 08, 2014 at 08:15 PM 3
Share

And if you want to trim the code a bit :

 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.P))
 {
    paused = !paused ;
    Time.timeScale = paused ? 0 : 1 ;
 }
avatar image TheBrokeBacks · May 15, 2014 at 06:27 PM 0
Share

Yeah, hahah I realized this after I posted the question. I felt like an idiot hahah

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

22 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

Related Questions

Problem changing TimeScale 1 Answer

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

Best practice for countdown timer on game resume 1 Answer

Stop force from being applied during pausescreen 1 Answer

Couldn't resume once Time.timeScale is set to 0 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