- Home /
 
Can't really pause my game
I did some digging around and I found out that when I set Time.timeScale equal to zero, it will stop only framerate independent stuff and that everything that is in the Update function will still run. Is there a way to pause the whole game? Should I disable the controls by setting the player scripts to Player_scr.SetActive(false)? Or is there a simpler, better way?
 public class GUI_scr : MonoBehaviour {
     
     public bool cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10;
     public bool[] cbxArr;
     public string player1, player2, player3, player4, player5, player6, player7, player8, player9, player10;
     public string[] playersArr;
     public string score1, score2, score3, score4, score5, score6, score7, score8, score9, score10;
     public string[] scoreArr;
 
 
     void Start()
     {
         cbxArr = new bool[]{cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9, cb10};
         scoreArr = new string[]{score1, score2, score3, score4, score5, score6, score7, score8, score9, score10};
         playersArr = new string[]{player1, player2, player3, player4, player5, player6, player7, player8, player9, player10};
 
         for(int i = 0; i<cbxArr.Length; i++)
         {cbxArr[i] = true;}
         
         for(int i = 0; i<scoreArr.Length; i++)
         {scoreArr[i] = "0";}
 
     }
 
 
     void OnGUI () {
         DrawGUI ();
     }
 
     public void DrawGUI()
     {
         GUI.BeginGroup (new Rect(0,0,100,768));
         for(int i = 0; i<10; i++)
         {
             GUI.BeginGroup(new Rect(0,60*i, 100,80));
             GUI.Box (new Rect(0,0,100,25), playersArr[i]);
             GUI.Box (new Rect(0,25,70,25), scoreArr[i].ToString ());
             cbxArr[i] = (GUI.Toggle (new Rect(80,30,300,30),cbxArr[i],""));
             GUI.EndGroup ();
         }
         GUI.EndGroup ();
 
         GUI.BeginGroup (new Rect((Screen.width-90),0,90,768));
         if(GUI.Button (new Rect(0,0,30, 20), "II"))
         {
             PauseGame ();
             // music OFF
         }
         if(GUI.Button (new Rect(30,0,30,20),"o"))
         {
             ResetGame ();
         }
         if(GUI.Button (new Rect(60,0,30,20),"x"))
         {
             QuitGame ();
         }
         GUI.EndGroup ();
     }
 
     void PauseGame()
     {
         float timeScale = (Time.timeScale != 0 ? Time.timeScale = 0 : Time.timeScale = 1);
     }
     void ResetGame()
     {
         Application.LoadLevel (0);
     }
     void QuitGame()
     {
         Application.Quit ();
     }
 }
 
               ------------- my player script ---------------
 public class Player_scr : MonoBehaviour {
 
     public int score;
 
     public float movementSpeed;
     private const string  idleAnimation = "idle-06";
     private const string walkAnimation = "walk-01";
     private const string attackAnimation = "attack-06";
     DmgCube_scr dmgCube_scr;
     GUI_scr gui_scr;
 
 
     public string moveHorizontal_plug;
     public string moveVertical_plug;
     public string fire_plug;
 
 
     protected virtual void Start () {
         score = 0;
         movementSpeed = 3f;
         animation[idleAnimation].layer = 1;
         animation[walkAnimation].layer = 1;
         animation[attackAnimation].layer = 2;
         gui_scr = GameObject.Find ("GUI").GetComponent<GUI_scr>();
         dmgCube_scr = transform.FindChild("dmgCube").GetComponent<DmgCube_scr>();
     }
     
 
     protected virtual void Update () {
     
         ControllPlayer();
 
 
     }
 
 
     public void ControllPlayer()
     {
         float moveHorizontal = Input.GetAxisRaw (moveHorizontal_plug);
         float moveVertical = Input.GetAxisRaw (moveVertical_plug);
 
         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
         if(movement != Vector3.zero)
         {
             animation.CrossFade (walkAnimation);
         }
         else if(!Input.GetButton (fire_plug))
         {
             animation.CrossFade (idleAnimation);
         }
 
         if(moveHorizontal != 0 || moveVertical != 0)
         {
             transform.rotation = Quaternion.LookRotation(movement);
         }
 
         transform.Translate (movement.normalized * movementSpeed * Time.deltaTime, Space.World);
 
         
         if(Input.GetButtonDown (fire_plug))
         {
             animation.Blend (attackAnimation);
             // play swing sound
             dmgCube_scr.Attack();
 
             Debug.Log ("gui_scr.scoreArr" + gui_scr.scoreArr[3]);
 
             // update players gui - score + 20gc;
 
         }
     }    
 
     public void AdjustScore(int adj)
     {
         score += adj;
 
         for(int i = 0; i<gui_scr.scoreArr.Length; i++)
         {
             if(gameObject.name == "mage" + (i+1).ToString ())
             {
                 Debug.Log ("gameObjectName" + gameObject.name);
                 gui_scr.scoreArr[i] = score.ToString ();
                 Debug.Log ("scoreWindow5 = " + gui_scr.scoreArr[i]);
                 // gui_scr.score1 = score.ToString ();
             }
         }
     }
 }
 
              Answer by PaxForce · Oct 25, 2014 at 06:17 AM
I've come up with a simple solution, which works. I don't know if this is the best way to resolve this problem, but since nobody else answered my question and I'm in kind of a hurry, I'll accept my own answer. I added a boolean gameControls to my ControllPlayer function.
 public void ControllPlayer()
     {
         if(gameControls)
         {
             float moveHorizontal = Input.GetAxisRaw (moveHorizontal_plug);
             float moveVertical = Input.GetAxisRaw (moveVertical_plug);
 
             Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
             if(movement != Vector3.zero)
             {
                 animation.CrossFade (walkAnimation);
             }
             else if(!Input.GetButton (fire_plug))
             {
                 animation.CrossFade (idleAnimation);
             }
 
             if(moveHorizontal != 0 || moveVertical != 0)
             {
                 transform.rotation = Quaternion.LookRotation(movement);
             }
 
             transform.Translate (movement.normalized * movementSpeed * Time.deltaTime, Space.World);
 
             
             if(Input.GetButtonDown (fire_plug))
             {
                 animation.Blend (attackAnimation);
                 // play swing sound
                 dmgCube_scr.Attack();
 
                 Debug.Log ("gui_scr.scoreArr" + gui_scr.scoreArr[3]);
 
                 // update players gui - score + 20gc;
             }
         }
     }    
 
              Your answer
 
             Follow this Question
Related Questions
Pause game that not using deltatime for movment 1 Answer
time.unscaled time help please? 1 Answer
Pause button 2 Answers
Pausing an Invoke for a time 2 Answers
Pause menu doesn't pause everything 0 Answers