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. Dismiss Notice

Problems pausing the game.

Discussion in 'Scripting' started by Yourking77, Jul 1, 2016.

  1. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    I have written a script that gives me no compiler errors and is basically teh same as a few of my other script that work, but I have no idea why this is not working. I think it has something to do with how I am telling it to function when you hit escape, I think I may be doing it wrong.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PauseScript : MonoBehaviour {
    6.  
    7.     public GameObject pauseMenu;
    8.     public GameObject gameUI;
    9.  
    10.     bool isPaused = false;
    11.  
    12.     void Update () {
    13.         if (isPaused == false)
    14.         {
    15.             if (Input.GetKeyDown(KeyCode.Escape))
    16.             {
    17.                 pause();
    18.             }
    19.         }
    20.         if (isPaused == true)
    21.         {
    22.             if (Input.GetKeyDown(KeyCode.Escape))
    23.             {
    24.                 resume();
    25.             }
    26.         }
    27.     }
    28.  
    29.     void pause()
    30.     {
    31.         Time.timeScale = 0.0f;
    32.         pauseMenu.SetActive(!pauseMenu.activeInHierarchy);
    33.         gameUI.SetActive(!gameUI.activeInHierarchy);
    34.         isPaused = true;
    35.     }
    36.  
    37.     void resume()
    38.     {
    39.         Time.timeScale = 1.0f;
    40.         pauseMenu.SetActive(!pauseMenu.activeInHierarchy);
    41.         gameUI.SetActive(!gameUI.activeInHierarchy);
    42.         isPaused = false;
    43.     }
    44. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    It needs to be an else if. Think that should fix it.
     
  3. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    Wow, definitely I overlooked that, thank you.

    I am still able to look around though any way to fix that?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    If you mean you're looking around in game with like a first person camera, you may have to disable the movement or simple don't respond to mouse movements when paused.
     
  5. Yourking77

    Yourking77

    Joined:
    Jan 14, 2016
    Posts:
    303
    yeah that is what I meant, I will test some things, thanks for the help. I cannot believe I overlooked something so simple..