Search Unity

Hide my HUD when Paused

Discussion in 'Scripting' started by OdyseeGames, Jul 7, 2020.

  1. OdyseeGames

    OdyseeGames

    Joined:
    Jun 24, 2020
    Posts:
    25
    Good Evening,

    I tried search on Google and this forum and haven't found much. I am trying to deactivate my Heads Up Display (HUD) when my Pause menu is activate and I cannot figure out how to do it. My pause menu is simple at this time, which I just copied from youtube tutorials. My HUD Canvas is called "HUD". I figured I have to some how call it into this script and setactive to false. But I cannot figure out how to reference the HUD canvas. My pause button is a button on the UI that you click on. So, I have it set to onclick to access the pause menu. I was also trying to see if it is possible to add another onclick action to the pause button that can deactivate the HUD canvas, but I couldn't figure that out either. Anyone have any recommendations on how to accomplish this or a tutorial that explains it as well? Thanks in advance!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3.  
    4.  
    5. public class PauseMenu : MonoBehaviour
    6. {
    7.  
    8.     public static bool GameIsPaused = false;
    9.    
    10.     public GameObject pauseMenuUI;
    11.  
    12.  
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.      
    17.     }
    18.  
    19.     public void Resume()
    20.         {
    21.             pauseMenuUI.SetActive(false);
    22.             Time.timeScale = 1f;
    23.             GameIsPaused = false;
    24.         }
    25.  
    26.       public void Pause()
    27.         {
    28.             pauseMenuUI.SetActive(true);
    29.             Time.timeScale = 0f;
    30.             GameIsPaused = true;
    31.                 }
    32.    
    33.  
    34.     public void LoadMenu()
    35.     {
    36.         Time.timeScale = 1f;
    37.         SceneManager.LoadScene("MainMenu");
    38.     }
    39.  
    40.     public void QuitGame()
    41.     {
    42.         Application.Quit();
    43.     }
    44.  
    45. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Normally I have a script that manages my UI and I can access parts of the UI through it. You could do this very thing, but I'm confused as to why you're running into issues. You have setup pauseMenuUI just fine in your code, so why couldn't you do something similar to the HUD?
     
  3. OdyseeGames

    OdyseeGames

    Joined:
    Jun 24, 2020
    Posts:
    25
    I'm just confused on the syntax. I'm still new at this so I dont know what to type.


     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    You already have the syntex. Create another GameObject variable, name it hud if you want. Then when you turn on pauseMenuUi, turn off the hud. And then when pauseMenuUI is turned off, turn the hud back on. Basically, duplicate what you have for the pauseMenuUI and just swap the order the get turned on. Don't forget to drag and drop the hud into the public variable in the inspector, just like you did with the pauseMenuUI like I assume.
     
  5. OdyseeGames

    OdyseeGames

    Joined:
    Jun 24, 2020
    Posts:
    25
    Thank you!