Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 nesma · Oct 24, 2014 at 03:24 PM · pausekeykeypresspausegame

Pause code wont work

hey I'm trying to implement a pause key for the game I'm creating, but for some reason it's not working, I've tried tagging it to the camera , a GUI text and Empty object none of that work. I've also tried different codes and they also won't work.

This is one of the codes I've tried :

 #pragma strict
 var paused : boolean = false;
 
 var pausedGUI : GUITexture;
 
 pausedGUI.enabled = false;
 
 function Update () {
 if(Input.GetKeyUp("p")) {
 
 if(paused == true)
 
    paused = false;
 
 } 
 
 else {
 paused = true;
 }
 
 if(paused == true)
 
 {
 Time.timeScale = 0.0;
 
 pausedGUI.enabled = true;
 }
 
 else{
 Time.timeScale = 1.0;
 
 pausedGUI.enabled = false;
 }
 
 }
 
 
 **This is another one**
 
 #pragma strict
 
 var isPaused : boolean = false;
 
 function Update() { 
 if(Input.GetKeyDown("p")) { 
 
 Pause(); 
 } 
 
 }
 
 function Pause()
 {
 
 if (isPaused == true)
 
 {
 Time.timeScale = 1;
 
 isPaused = false;
 }
 
 else
 {
 Time.timeScale = 0;
 
 isPaused = true;
 }
 
 }

Am I doing something wrong ?

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 Habitablaba · Oct 24, 2014 at 06:20 PM

I cleaned up both versions of your script a bit, but overall they look like they should work just fine. I wonder if you don't have this script on more than one object? If so, you should not do that.
As a personal preference, I like the second version of the script better as it will only check the status of the paused variable when it has the potential to be different (when the player presses 'P'). Whereas the first script will set the time scale every frame.

 #pragma strict
 
 var paused : boolean = false;
 var pausedGUI : GUITexture;
 pausedGUI.enabled = false;
 
 function Update () {
   if(Input.GetKeyDown(KeyCode.P)){
     paused = !paused;
   }
 
   if(paused)
   {
     Time.timeScale = 0.0;
     pausedGUI.enabled = true;
   }
   else{
     Time.timeScale = 1.0;
     pausedGUI.enabled = false;
   }
 }
 
 **This is another one**
 #pragma strict
 
 var isPaused : boolean = false;
 
 function Update() {
   if(Input.GetKeyDown(KeyCode.P)) {
     Pause();
   }
 }
 
 function Pause()
 {
   if (isPaused)
   {
     Time.timeScale = 1;
   }
   else
   {
     Time.timeScale = 0;
   }
 
   isPaused = !isPaused;
 }
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 nesma · Oct 24, 2014 at 06:25 PM 0
Share

yeah you're right ^^ I've attached the code only to the main camera , it didn't work , I've tried attaching it to an empty object as well that didn't work either, I'm starting to think that I should attach it to each object but I know that's not right

avatar image Habitablaba · Oct 24, 2014 at 06:34 PM 0
Share

Oh hey, here's a question: where are you handling the movement of your other objects? Setting the timescale to 0 will not prevent Update() from being called. If you have movement code in Update, that will still get executed. Try moving your movement code to FixedUpdate and see what that does for you.

avatar image nesma · Oct 24, 2014 at 06:37 PM 0
Share

I'll give it a go thannk youu ^^

avatar image Habitablaba · Oct 24, 2014 at 06:47 PM 0
Share

for more, and how to make it work in Update ins$$anonymous$$d of FixedUpdate (which you don't need if you aren't doing physics) check out the accepted answer here.
Basically, if you aren't scaling your movement by delta time, you should be, then you can move the code back to Update from FixedUpdate.

avatar image Habitablaba · Oct 24, 2014 at 07:57 PM 1
Share

Don't forget to swing back around to let us know if it worked, and to mark the question as answered.

Show more comments
avatar image
0

Answer by 8Eye · Oct 24, 2014 at 04:41 PM

This is how to do it in c#. It will be easy for you to port to JS.

    public bool paused = false;
     
     if(Input.GetKeyUp(KeyCode.P)){
     
        paused = !paused //toggles between paused and not paused
     }
     
     if(paused){
     
        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 nesma · Oct 24, 2014 at 05:59 PM 0
Share

Still not working :( the objects in the scene are still moving alt text

when I enter the game scene and press play this is the only thing that's changing , but the objects in the scene itself are still moving

capture.png (1.2 kB)

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

29 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Door Open With Object Pickup 1 Answer

How to pause game. 3 Answers

Anyway to get what key is down? 1 Answer

Create GUIText by pressing key 1 Answer

How to Rotate Game Object in specific Angle repeatedly on Key Press? 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