Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 pommerose94 · Jan 09, 2020 at 08:08 AM · pause game

problem with pause button using time.timescale

i am new and creating a 2D Game i created a pause button that on click activates a pause menu it works. However, when i added this script, the players stop moving, but they can still turn left, right, and when i press shoot button during pause, my player sprite changes to shoot and the bullet appears, he does not move, you can play like this, but it is bizzare I want a script that pauses completely my script and WITHOUT time.TimeScale i tried 10 different ones and none would work.

my script:

 using UnityEngine;
  using UnityEngine.UI;  
  public class PauseGame : MonoBehaviour {
      void Start() {
          GetComponent<Button>().onClick.AddListener(TogglePause);
      }
      public void TogglePause() {
          Time.timeScale = Mathf.Approximately(Time.timeScale, 0.0f) ? 1.0f : 0.0f;        
      }
  }
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 ashishcw · Jan 09, 2020 at 12:46 PM 0
Share

Simple way to do that, add a single boolean in your pause script, and check that boolean, where you have your player and other script calling to.

Now, simply check, if the pause menu is activated.

e.g. I assume your player and controller scripts have update function ``` void Update() { if (!Pause_$$anonymous$$enu.pause_menu_activated){//Rest of the player code goes here}

} ```

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by ShadyProductions · Jan 09, 2020 at 12:43 PM

Setting timescale to 0 only stops physics and update calls that are framerate independent. It does however not stop people from clicking UI buttons or rotating or doing other stuff that does not involve time calculation like instantiating objects etc.

What you can do is have a static variable that verifies if the game is paused:

 public static bool IsGamePaused { get { return Time.timeScale == 0; } }

Then for every script you have you will need to check IsGamePaused if so you have to return from the method that does your calculations.

Example:

 public void ShootBullet() {
     if (IsGamePaused) return;
     // other logic
 }
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
avatar image
0

Answer by pommerose94 · Jan 09, 2020 at 03:45 PM

I am very new @ShadyProductions I did not completely understood the second script, what is //other logic representing??? and i bind the first script to my PauseGame script, right?

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 ashishcw · Jan 09, 2020 at 03:53 PM 0
Share

The first part what @ShadyProductions mentioned, is not a script, but rather a static variable which will check if the game is paused or not.

In a very simple terms, you can set the Time.timescale = 0 when player pauses the game, and just add IsGamePaused as a if condition to the rest of your functions which are related to bullets shooting and playermovent etc, Unity will take care the rest of the backend operations.

avatar image ShadyProductions · Jan 10, 2020 at 09:51 AM 1
Share

Seems you should learn some basic C# before you attempt unity.

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

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

Related Questions

How to make a pause menu with Gui Texture?? 2 Answers

I want OnClick to fire the same frame that the button is clicked 1 Answer

How do I unpause my game using Time.timeScale? 1 Answer

Problem when switching between scenes 0 Answers

Pause game but not Canvas and some effect 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