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);
}
}
how to make all game objects stop when pause button pressed ?!!
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.