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

I need help with pause menu

Discussion in 'Scripting' started by afs3122, Dec 18, 2020.

  1. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    I made a 1st person camera and a pause menu but when testing when I pause my mouse is gone because its 1st person game therefore I cant click and when I put
    Code (CSharp):
    1. if (PauseMenu.GameIsPaused = true) {
    2.             Cursor.lockState = CursorLockMode.None;
    3.             Cursor.visible = true;
    4.         }
    in the camera script, it just spams the GameIsPaused = true in the console and when I try to open the pause menu it freezes my game like its supposed to but the UI doesn't show up but when I remove it and test it it spams GameIsPaused = false and I can open the pause menu like normal but can;t see the mouse

    Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PauseMenu : MonoBehaviour {
    6.    
    7.     public static bool GameIsPaused = false;
    8.  
    9.     public GameObject pauseMenuUI;
    10.    
    11.     //Update is called once per frame
    12.     void Update() {
    13.         Debug.Log(GameIsPaused);
    14.  
    15.         if (Input.GetKeyDown(KeyCode.Escape))
    16.         {
    17.             if  (GameIsPaused)
    18.             {
    19.                 Resume();
    20.             }  else
    21.             {
    22.                 Pause();
    23.             }
    24.         }
    25.     }
    26.  
    27.     public void Resume ()
    28.     {
    29.         pauseMenuUI.SetActive(false);
    30.         Time.timeScale = 0f;
    31.         GameIsPaused = false;
    32.     }
    33.  
    34.     public void Pause ()
    35.     {
    36.         pauseMenuUI.SetActive(true);
    37.         Time.timeScale = 0f;
    38.         GameIsPaused = true;
    39.     }
    40.  
    41.  
    42.  
    43. }
    44.  
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    You wrote = (single-equals, the assignment operator) where you meant == (double-equals, the equality test)
     
  3. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    OHHHHHH
     
  4. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    The other thing I dont get is that when I try to click the buttons in the pause menu my mouse just disappears and I cant get the mouse back unless I close the pause menu (sec) and then reopen it
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You set timeScale to 0 both in resume and pause, i think you mean resume to set it to 1(or the timescale used before pause was called)
     
  6. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    I just fixed that but when I pause the game while testing and try to press the button(Resume,Menu,Quit) my mouse just disappears and I cant get it back unless I unpause it then repause it
     
  7. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    That sounds like a totally separate problem that would require a completely new set of information to solve.
     
    SparrowGS likes this.
  8. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    why not set cursor state in the pause and resume functions?

    where is this and where else do you set cursor state?
    Code (CSharp):
    1. if (PauseMenu.GameIsPaused = true) {
    2.             Cursor.lockState = CursorLockMode.None;
    3.             Cursor.visible = true;
    4.         }
    5.  
     
  9. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    I put it in my script for my camera

    Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour
    6. {
    7.     public float mouseSensitivity = 100f;
    8.  
    9.     public Transform playerBody;
    10.  
    11.     float xRotation = 0f;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.  
    17.         Cursor.lockState = CursorLockMode.Locked;
    18.         if (PauseMenu.GameIsPaused == true) {
    19.             Cursor.lockState = CursorLockMode.None;
    20.             Cursor.visible = true;
    21.         }
    22.     }
    23.  
    24.     // Update is called once per frame
    25.     void Update()
    26.     {
    27.        
    28.  
    29.  
    30.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    31.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    32.  
    33.     xRotation -= mouseY;
    34.     xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    35.  
    36.  
    37.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    38.         playerBody.Rotate(Vector3.up * mouseX);
    39.     }
    40.  
     
  10. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    you only do it on start, and you only change it from unpaused to paused.
    either change the cursor in the pause and resume functions or move this check to update and take the other case into account.
     
  11. afs3122

    afs3122

    Joined:
    Dec 9, 2020
    Posts:
    13
    Well when I did that and I pause and its fine but if I unpause by pressing esc not the resume button I can still see my mouse