Search Unity

Pause Menu and Inventory problem!

Discussion in 'Scripting' started by Doomer022, Jun 29, 2018.

  1. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Hello everyone! I have been making a smaller open world sandbox building game, and I implemented a pause menu and an inventory menu. But its not working the way I wanted it to. When In the Inventory menu, the cursor wont appear, and it also wont pause the game while opening it. I can still look around and build/destroy blocks, but the menu is still open. The pause menu is somewhart better, pressing ESC will stop the game and the cursor will be there, but clicking on anything will lock the cursor back to the middle while its still visible. And i can destroy objects on both menus, while I coded it in a way so when the Inventory and pause menu bool is true, it shouldnt destroy objects, but it does. Any ideas what to do here?

    Inventory Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Inventory : MonoBehaviour {
    7.  
    8.     public GameObject InventoryUI;
    9.     public static bool InvUse = false;
    10.  
    11.     void Update()
    12.     {
    13.         if (Input.GetKeyDown("e")) {
    14.             InvUse = !InvUse;
    15.         }
    16.         if (InvUse == true)
    17.         {
    18.             Cursor.visible = true;
    19.             Cursor.lockState = CursorLockMode.None;
    20.             Time.timeScale = 0f;
    21.             InventoryUI.SetActive(true);
    22.         }
    23.         if (InvUse == false)
    24.         {
    25.             Time.timeScale = 1f;
    26.             Cursor.visible = false;
    27.             Cursor.lockState = CursorLockMode.Locked;
    28.             InventoryUI.SetActive(false);
    29.         }
    30.     }
    Pause menu Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PauseMenu : MonoBehaviour {
    7.  
    8.     public static bool gameIspaused = false;
    9.  
    10.     public GameObject PMUI;
    11.    
    12.     void Update () {
    13.         if (Input.GetKeyDown(KeyCode.Escape))
    14.         {
    15.             gameIspaused = !gameIspaused;
    16.         }
    17.         if (gameIspaused == true)
    18.         {
    19.             Resume();
    20.         }
    21.         else if (gameIspaused == false)
    22.         {
    23.             Pause();
    24.         }
    25.     }
    26.     public void LoadMenu()
    27.     {
    28.         Time.timeScale = 0f;
    29.         SceneManager.LoadScene(0);
    30.         Cursor.visible = true;
    31.     }
    32.     public void Resume()
    33.     {
    34.         Time.timeScale = 0f;
    35.         Cursor.visible = true;
    36.         Cursor.lockState = CursorLockMode.None;
    37.         PMUI.SetActive(true);
    38.     }
    39.     public void Pause()
    40.     {
    41.         Time.timeScale = 1f;
    42.         Cursor.visible = false;
    43.         Cursor.lockState = CursorLockMode.Locked;
    44.         PMUI.SetActive(false);
    45.     }
    46. }
    47.  
    Block destroying script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class BlockDestroyer : MonoBehaviour {
    6.  
    7.     void Start()
    8.     {
    9.         //Screen.lockCursor = true;
    10.     }
    11.     void OnMouseDown()
    12.     {
    13.         if (GameFlow.buildMode == false && Inventory.InvUse == false)
    14.         {
    15.             Destroy(gameObject);
    16.         }
    17.         if (GameFlow.buildMode == true && Inventory.InvUse == false)
    18.         {
    19.             GameFlow.currentTile = transform.position;
    20.         }
    21.     }
    22. }
    23.  
    I know, this post is pretty big like this and time consuming to fix, but please be so kind and help me out! Thank you! :)
     
  2. Hambanana

    Hambanana

    Joined:
    Jul 3, 2013
    Posts:
    9
    To begin, I would look into get set. It will really help clean up your scripts. You shouldn't be running most of the code continuously in the Update function. Try something like this. More information is needed to really know what is going on.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PauseMenu : MonoBehaviour
    7. {
    8.  
    9.     private bool gameIspaused = false;
    10.     public bool GameIsPaused
    11.     {
    12.         get
    13.         {
    14.             return gameIspaused;
    15.         }
    16.         set
    17.         {
    18.             gameIspaused = value;
    19.             PauseState(value);
    20.         }
    21.     }
    22.  
    23.     public GameObject PMUI;
    24.  
    25.     void Update()
    26.     {
    27.         if (Input.GetKeyDown(KeyCode.Escape))
    28.         {
    29.             GameIsPaused = !GameIsPaused;
    30.         }
    31.     }
    32.     void PauseState(bool isPaused) {
    33.         Cursor.visible = isPaused;
    34.         PMUI.SetActive(isPaused);
    35.         if (isPaused)
    36.         {
    37.             Cursor.lockState = CursorLockMode.None;
    38.             Time.timeScale = 0f;
    39.         }
    40.         else {
    41.             Time.timeScale = 1f;
    42.             Cursor.lockState = CursorLockMode.Locked;
    43.         }
    44.     }
    45.     public void LoadMenu()
    46.     {
    47.         Time.timeScale = 0f;
    48.         SceneManager.LoadScene(0);
    49.         Cursor.visible = true;
    50.     }
    51. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Inventory : MonoBehaviour
    6. {
    7.  
    8.     public GameObject InventoryUI;
    9.     private bool invUse = false;
    10.  
    11.     public bool InvUse
    12.     {
    13.         get
    14.         {
    15.             return invUse;
    16.         }
    17.         set
    18.         {
    19.             invUse = value;
    20.             InventoryState(value);
    21.         }
    22.     }
    23.     void Update()
    24.     {
    25.         if (Input.GetKeyDown("e"))
    26.         {
    27.             InvUse = !InvUse;
    28.         }
    29.     }
    30.     void InventoryState(bool isActive) {
    31.         Cursor.visible = isActive;
    32.         InventoryUI.SetActive(isActive);
    33.         if (isActive)
    34.         {
    35.             Time.timeScale = 0f;
    36.             Cursor.lockState = CursorLockMode.None;
    37.         }
    38.         else {
    39.             Time.timeScale = 1f;
    40.             Cursor.lockState = CursorLockMode.Locked;
    41.         }
    42.     }
    43. }
    44.