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 /
  • Help Room /
avatar image
1
Question by tarooq636 · Oct 09, 2015 at 02:00 PM · c#pause

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

i created a pause button and dragged the script to the button then when i press the button while playing the game i'v pressed the pause button everything stops and i mean by everything : dying when approach from obstacles and picking up coins but the enemies keeps moving and the player also

here is my pause code

 using UnityEngine;
 using System.Collections;
 
 public class PauseGame : MonoBehaviour {
     public bool paused;
     // Use this for initialization
     void Start () {
         paused = false;
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 
     public void Pause (){
 
         paused = !paused;
         if (paused) {
 
             Time.timeScale = 0;
         }
 
         else if(!paused) {
 
             Time.timeScale = 1;
         }
     }
 }
 

here is my player controller script using UnityEngine; using System.Collections;

 public class PlayerController : MonoBehaviour {
     public float jumpPower;
     public float speed;
     public bool moveLeft;
     public bool moveRight;
     public Rigidbody2D rigidbody2D;
     public Animator animator;
     public ScoreManager scoreManager;
 
 
     // Use this for initialization
     void Start () {
         rigidbody2D = gameObject.GetComponent<Rigidbody2D> ();
 
     }
     
     // Update is called once per frame
     void Update () {
         
         
         if (moveLeft)
             MoveLeft ();
         if (moveRight)
             MoveRight ();
         
     }
     public void MoveLeft()
         
     {
         transform.position = new Vector3 (transform.position.x - speed, transform.position.y, transform.position.z);
         animator.Play ("Walk");
         transform.localScale = new Vector3 (-.75f, transform.localScale.y, transform.localScale.z);
         
     }
     
     public void MoveRight()
     {
         transform.position = new Vector3 (transform.position.x + speed, transform.position.y, transform.position.z);
         animator.Play ("Walk");
         transform.localScale = new Vector3 (.75f, transform.localScale.y, transform.localScale.z);
         
     }
     
     public void ControlMoveLeftButton(bool val)
     {
         moveLeft = val;
         if (!val)
             animator.Play ("Idle");
     }
     public void ControlMoveRightButton(bool val)
     {
         moveRight = val;
         if (!val)
             animator.Play ("Idle");
     }
     public void Jump(){
         //rigidbody2D.AddForce (Vector3.up * jumpPower);
         rigidbody2D.AddForce (new Vector2 (0, 10), ForceMode2D.Impulse);
     }
     void OnTriggerEnter2D (Collider2D other)
     {
         if (other.tag == "Plastic") {
             Destroy (other.gameObject);
             scoreManager.IncreasePlasticCoins ();        }
         if (other.tag == "Light") {
             Destroy (other.gameObject);
             scoreManager.IncreaseLightCoins ();
         }
         
         if (other.tag == "Heavy") {
             Destroy (other.gameObject);
             scoreManager.IncreaseHeavyCoins ();
         }
         
         if (other.tag == "Ameba") {
             Destroy (other.gameObject);
             scoreManager.LostFiveCoins();
         } 
         
         if (other.tag == "Anthrax") {
             Destroy (other.gameObject);
             scoreManager.LostTenCoins();
         } 
         
         if (other.tag == "Dangerous") {
             animator.Play ("Die");
             Invoke("GameOver", 0);
         }
     }
     void GameOver() {
         Application.LoadLevel("GameOver");
         
     }
     
 }
 

and the enemy controller

 using UnityEngine;
 using System.Collections;
 
 public class EnemyController : MonoBehaviour {
     public float speed;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () 
     
     {
         transform.position = new Vector3 (transform.position.x - speed, transform.position.y, transform.position.z);
 
 
     }
 
 } 
 

Comment
Add comment · Show 2
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 FortisVenaliter · Oct 09, 2015 at 02:45 PM 0
Share

So.... where's the question?

avatar image tarooq636 FortisVenaliter · Oct 09, 2015 at 06:28 PM 0
Share

how to make all game objects stop when pause button pressed ?!!

1 Reply

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

Answer by DyrdaOrg · Oct 29, 2015 at 11:59 AM

Hi @tarooq636 I had the same problem... I was pausing game but some crates were still jumping. The problem is in transform.position Wherever you're using it void Update() void MoveLeft() etc. you need to add:

 if (Time.timeScale != 0){
  
  }

so it'll look like (ie. void MoveLeft):

      public void MoveLeft()
      {
 
 if(Time.timeScale !=0) {
 
          transform.position = new Vector3 (transform.position.x - speed, transform.position.y, transform.position.z);
          animator.Play ("Walk");
          transform.localScale = new Vector3 (-.75f, transform.localScale.y, transform.localScale.z);
       }
 }

Should work like a charm. Let me know if it helped you and if it did then upvote my answer, please, so other my benefit from this solution in the future.

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 tarooq636 · Oct 29, 2015 at 08:09 PM 0
Share

worked like a charm! Thanks

avatar image RHD · Aug 29, 2016 at 08:41 PM 0
Share

Thank You!

You are a God!

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

5 People are following this question.

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

Make my moving platforms pause at its endpoints using c# 2 Answers

How to program a pause/unpause button in 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