Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Pausing Game

Discussion in 'Scripting' started by chemsoun, Jun 8, 2015.

  1. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    hi everyone

    i'm trying to pause my game in unity, i managed to pause it but i can't get to restrat it even if my code seems

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CollecterState : MonoBehaviour {
    5.  
    6.     private Animator animator;
    7.     private CharacterController controlleur;
    8.  
    9.     public Transform effect;
    10.  
    11.     public float speed = 6.0f;
    12.     public float turnSpeed = 60.0f;
    13.     private Vector3 moveDirection = Vector3.zero;
    14.     public float gravity = 20.0f;
    15.  
    16.     private int score;
    17.     public float TimeLeft;
    18.     private float TimeAdded = 3f;
    19.     private float TimeDeduction = 2f;
    20.    
    21.     private bool GameOver = false;
    22.     private int nbmauvais = 0;
    23.  
    24.    
    25.     void OnGUI(){
    26.        
    27.        
    28.         GUI.Box (new Rect(33,23,85,25),"Score : " + score);
    29.         GUI.Box(new Rect(1110,23,152,29),"Temps Restant : " +(int)TimeLeft );
    30.  
    31.         if (GameOver) {
    32.        
    33.             Time.timeScale=0;
    34.             nbmauvais=0;
    35.  
    36.  
    37.             GUI.Box(new Rect(539,163,238,200),"Collector");
    38.             if(GUI.Button(new Rect(569,200,181,38),"Rejouer")){
    39.  
    40.                 Application.LoadLevel(Application.loadedLevel);
    41.  
    42.             }
    43.  
    44.             if(GUI.Button(new Rect(569,258,181,38),"Quitter")){
    45.  
    46.                 Time.timeScale=1;
    47.                 Application.LoadLevel("MainMenu");
    48.  
    49.             }
    50.        
    51.         }
    52.  
    53.    
    54.     }
    55.  
    56.     // Use this for initialization
    57.     void Start () {
    58.        
    59.         animator = gameObject.GetComponentInChildren<Animator> ();
    60.         controlleur = GetComponent <CharacterController> ();
    61.         Time.timeScale = 1;
    62.        
    63.         Debug.Log(Application.loadedLevelName);
    64.        
    65.     }
    66.    
    67.     // Update is called once per frame
    68.     void Update () {
    69.    
    70.         if (Input.GetKey("up") || Input.GetKey("right")  || Input.GetKey("left")) {
    71.  
    72.             animator.SetInteger ("run", 1);
    73.  
    74.             if (controlleur.isGrounded) {
    75.                 moveDirection = transform.forward * Input.GetAxis ("Vertical") * speed;
    76.             }
    77.  
    78.             float turn = Input.GetAxis("Horizontal");
    79.             transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
    80.             controlleur.Move(moveDirection * Time.deltaTime);
    81.             moveDirection.y -= gravity * Time.deltaTime;
    82.  
    83.        
    84.         } else {
    85.        
    86.             animator.SetInteger("run",0);
    87.        
    88.         }
    89.  
    90.  
    91.        
    92.         TimeLeft -= Time.deltaTime;
    93.        
    94.         if (TimeLeft <= 0.0) {
    95.            
    96.             Time.timeScale=0;
    97.             GameOver=true;
    98.             Debug.Log("Time Up");
    99.             //TimeLeft = 0.0f;
    100.  
    101.         }
    102.  
    103.     }
    104.    
    105.     void OnTriggerEnter(Collider col){
    106.        
    107.         if (col.gameObject.tag == "energie") {
    108.             score ++;
    109.             TimeLeft+=TimeAdded;
    110.             Instantiate(effect, transform.position,transform.rotation);
    111.             Destroy(col.gameObject);
    112.             Debug.Log(col.gameObject.name);
    113.         }
    114.         else if (col.tag == "non") {
    115.            
    116.             TimeLeft-=TimeDeduction;
    117.             nbmauvais++;
    118.             Instantiate(effect, transform.position,transform.rotation);
    119.             Destroy(col.gameObject);
    120.             if(nbmauvais == 5){
    121.                
    122.                 GameOver=true;
    123.                
    124.             }
    125.            
    126.         }
    127.        
    128.     }
    129. }
    130.  
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    I see from your code that you're setting Time.timeScale to 0 (pausing) when the user runs out of time, which makes GameOver true, which in turn makes the Update function set Time.timeScale to 0... If the user presses the "Quitter" button, you attempt to set Time.timeScale to 1 (unpausing) and then load the Main Menu, but the GameOver variable is still set to true, so the next update function will likely just set Time.timeScale right back to 0, repausing the game. The only other place that I see you setting Time.timeScale to 1 is in the Start function... I'm not entirely sure it's what you're after, but try this altered version of your script and let me know if you have any better luck with it...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CollecterState : MonoBehaviour {
    5.    
    6.     private Animator animator;
    7.     private CharacterController controlleur;
    8.    
    9.     public Transform effect;
    10.    
    11.     public float speed = 6.0f;
    12.     public float turnSpeed = 60.0f;
    13.     private Vector3 moveDirection = Vector3.zero;
    14.     public float gravity = 20.0f;
    15.    
    16.     private int score;
    17.     public float TimeLeft;
    18.     private float TimeAdded = 3f;
    19.     private float TimeDeduction = 2f;
    20.    
    21.     private bool GameOver = false;
    22.     private int nbmauvais = 0;
    23.    
    24.    
    25.     void OnGUI(){
    26.        
    27.        
    28.         GUI.Box (new Rect(33,23,85,25),"Score : " + score);
    29.         GUI.Box(new Rect(1110,23,152,29),"Temps Restant : " +(int)TimeLeft );
    30.        
    31.         if (GameOver) {
    32.            
    33.             Time.timeScale=0;
    34.             nbmauvais=0;
    35.            
    36.            
    37.             GUI.Box(new Rect(539,163,238,200),"Collector");
    38.             if(GUI.Button(new Rect(569,200,181,38),"Rejouer")){
    39.                
    40.                 Application.LoadLevel(Application.loadedLevel);
    41.                
    42.             }
    43.            
    44.             if(GUI.Button(new Rect(569,258,181,38),"Quitter")){
    45.                
    46.                 GameOver = false; // set GameOver to false so the game won't keep pausing
    47.                Time.timeScale=1; // set the time scale back to 1, unpausing the game
    48.                 Application.LoadLevel("MainMenu"); // and load the main menu
    49.                
    50.             }
    51.            
    52.         }
    53.        
    54.        
    55.     }
    56.    
    57.     // Use this for initialization
    58.     void Start () {
    59.        
    60.         animator = gameObject.GetComponentInChildren<Animator> ();
    61.         controlleur = GetComponent <CharacterController> ();
    62.         Time.timeScale = 1;
    63.        
    64.         Debug.Log(Application.loadedLevelName);
    65.        
    66.     }
    67.    
    68.     // Update is called once per frame
    69.     void Update () {
    70.  
    71.         if (Input.GetKey("up") || Input.GetKey("right")  || Input.GetKey("left")) {
    72.            
    73.             animator.SetInteger ("run", 1);
    74.            
    75.             if (controlleur.isGrounded) {
    76.                 moveDirection = transform.forward * Input.GetAxis ("Vertical") * speed;
    77.             }
    78.            
    79.             float turn = Input.GetAxis("Horizontal");
    80.             transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
    81.             controlleur.Move(moveDirection * Time.deltaTime);
    82.             moveDirection.y -= gravity * Time.deltaTime;
    83.            
    84.            
    85.         } else {
    86.            
    87.             animator.SetInteger("run",0);
    88.            
    89.         }
    90.        
    91.        
    92.        
    93.         TimeLeft -= Time.deltaTime;
    94.        
    95.         if (TimeLeft <= 0.0) {
    96.            
    97.             // Time.timeScale=0; // this isn't needed since Update set's Time.timeScale to 0 when GameOver is true, which happens on the next line...
    98.             GameOver=true;
    99.             Debug.Log("Time Up");
    100.             //TimeLeft = 0.0f;
    101.            
    102.         }
    103.        
    104.     }
    105.    
    106.     void OnTriggerEnter(Collider col){
    107.        
    108.         if (col.gameObject.tag == "energie") {
    109.             score ++;
    110.             TimeLeft+=TimeAdded;
    111.             Instantiate(effect, transform.position,transform.rotation);
    112.             Destroy(col.gameObject);
    113.             Debug.Log(col.gameObject.name);
    114.         }
    115.         else if (col.tag == "non") {
    116.            
    117.             TimeLeft-=TimeDeduction;
    118.             nbmauvais++;
    119.             Instantiate(effect, transform.position,transform.rotation);
    120.             Destroy(col.gameObject);
    121.             if(nbmauvais == 5){
    122.                
    123.                 GameOver=true;
    124.                
    125.             }
    126.            
    127.         }
    128.        
    129.     }
    130. }
    I can think of a couple of more elegant ways to approach it, but figured I'd leave most of what you had intact and just work around it. Let me know how it works for you as I haven't tested it.
     
  3. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    well thanks for your reply, i have ttried your code but unfortunatly it still doesn't work and gives the same result as my code, i'm thinking that the problem is in the line

    Code (CSharp):
    1. Application.LoadLevel(Application.loadedLevel);
    as the player presses the "Rejouer" button which means restart, it should just re-load the same level as if it's the first time .

    what do you think about that
     
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Well, it should reload the current level (works for me, I use it in a few games). Are you having trouble with that command not restarting your level, or is your problem with the pause function this thread is about?
     
  5. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    yes, the problem is restarting the level :p
     
  6. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Okay, does the button simply do nothing, or does the Debug line from your Start function fire after you hit the button? In other words, after you hit the Restart button, do you see the Debug.Log(Application.loadedLevelName); output in your console (it should show the name of the currently loaded level). I ask simply because I'm trying to discern if nothing is happening at all, or if your Start function is firing at all. If it's the former, we'll have to do some poking and prodding to figure out what the issue is. If it's the latter, then it's likely simply a matter of resetting all of your variable values in the Start function. Since they're not being reset, other than Time.timeScale, this may be causing problems.
     
  7. chemsoun

    chemsoun

    Joined:
    Apr 9, 2015
    Posts:
    49
    indeed, pressing the Restart button fires the Start function and it shows the Debug output in the console
     
  8. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Okay, in that case, the issue is most likely with needing to reset variable values, positions, things like that. You can expand your Start function to include all of this and, if necessary, reset the position of the player, etc.
     
  9. PerfectlyInsane

    PerfectlyInsane

    Joined:
    Aug 9, 2013
    Posts:
    74
    When gameover becomes true it will keep on setting timescale = 0 every frame.
     
    krougeau likes this.
  10. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Great catch. I feel silly having forgotten that OnGUI functions just like Update does. If the OP adds a
    Code (CSharp):
    1. GameOver = false;
    line to his "Rejouer" segment, it should work out nicely. Thanks!