How can I hide all the elements inside of a canvas?

I need to hide the current scene contents when the user clicks a button. The button will open another scene.

My code:

public class OpenCredits : MonoBehaviour
{
    public void OpenCreditScene()
    {
        SceneManager.LoadScene("Credits", LoadSceneMode.Additive);
        MainMenu.SetActive(false);  // This is not working. MainMenu is the canvas and has some buttons and TextMeshPro objects in it.
    }
}

Thanks.

@MichaelA1337 Here is my “Pause Game Menu” script. Is quite long, but I hope it helps! In my Canvas I have created an EMPTY OBJECT and named it “PauseGameMenu”. I have attached this script to it. I build all my inner menu panels as children of the empty-pausegamemenu object. Here’s my script:

using UnityEngine;
using UnityEngine.SceneManagement;

public class PauseGameMenu : MonoBehaviour
{
    [Header("Main Game Music")]
    public AudioSource mainGameMusic;
    [Header("Pause Menu Music")]
    public AudioSource pauseMenuMusic;
    [Header("ButtonPressSound")]
    public AudioSource pressButtonSound;
    [Header("Is the PauseGameMenu Open?")]
    public bool pauseGameMenu = false;
    [Header("PauseGameMenu in Canvas")]
    public GameObject pauseMenu;
    [Header("Turn OFF the Player")]
    public GameObject thePlayer;
    [Header("Player Name Display Off")]
    public GameObject playerNameDisplay;
    [Header("Turn OFF Save Position")]
    public GameObject savePositionButton;
    [Header("Turn OFF Load Position")]
    public GameObject loadPositionButton;
    [Header("Items Panel in Canvas")]
    public GameObject itemPanel;
    [Header("House Items Panel in Canvas")]
    public GameObject itemHousePanel;
    [Header("Pet Items Panel in Canvas")]
    public GameObject itemPetPanel;
    [Header("Player Items Panel in Canvas")]
    public GameObject itemPlayerPanel;
    [Header("Current Quests Panel in Canvas")]
    public GameObject currentQuestsPanel;
    [Header("All Quests Panel in Canvas")]
    public GameObject allQuestsPanel;
    [Header("Q001 Info Button in Canvas")]
    public GameObject Q001PickFlowerSwordButton;
    [Header("Q002 Info Panel in Canvas")]
    public GameObject Q002KillTheSpidersButton;
    [Header("Q003 Info Panel in Canvas")]
    public GameObject Q003CatchHurtButterflyButton;
    [Header("Q001 Info Panel in Canvas")]
    public GameObject Q001InfoPanel;
    [Header("Q002 Info Panel in Canvas")]
    public GameObject Q002InfoPanel;
    [Header("Q003 Info Panel in Canvas")]
    public GameObject Q003InfoPanel;
    [Header("Stats Panel in Canvas")]
    public GameObject statPanel;


    // Update is called once per frame
    void Update()
    {
        if (Input.GetButtonDown("Cancel"))
        {
            if (pauseGameMenu == false)
            {
                pressButtonSound.Play();
                pauseMenuMusic.Play();
                mainGameMusic.Pause();
                Time.timeScale = 0;
                pauseGameMenu = true;
                Cursor.visible = true;
                pauseMenu.SetActive(true);
                thePlayer.GetComponent<CubeMovement>().enabled = false;
                playerNameDisplay.SetActive(false);
                savePositionButton.SetActive(false);
                loadPositionButton.SetActive(false);
            }
            else
            {
                pauseMenu.SetActive(false);
                mainGameMusic.UnPause();
                pauseMenuMusic.Pause();
                Cursor.visible = true;
                pauseGameMenu = false;
                Time.timeScale = 1;
                thePlayer.GetComponent<CubeMovement>().enabled = true;
                playerNameDisplay.SetActive(true);
                savePositionButton.SetActive(true);
                loadPositionButton.SetActive(true);
            }
        }
    }

    // All Buttons Go Here

    //Main "Items Button" on far left
    public void ShowItem()
    {
        itemPanel.SetActive(true);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    //House Items Button within the Main Items Button
    public void ShowHouseItems()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(true);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    //Pet Items Button within the Main Items Button
    public void ShowPetItems()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(true);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    //Player Items Button within the Main Items Button
    public void ShowPlayerItems()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(true);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    public void ShowCurrentQuest()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(true);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    public void ShowAllQuestsList()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(true);
        Q001PickFlowerSwordButton.SetActive(true);
        Q002KillTheSpidersButton.SetActive(true);
        Q003CatchHurtButterflyButton.SetActive(true);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    public void ShowQ001InfoPanel()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(true);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    public void ShowQ002InfoPanel()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(true);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(false);
    }

    public void ShowQ003InfoPanel()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001PickFlowerSwordButton.SetActive(false);
        Q002KillTheSpidersButton.SetActive(false);
        Q003CatchHurtButterflyButton.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(true);
        statPanel.SetActive(false);
    }

    public void ShowStat()
    {
        itemPanel.SetActive(false);
        itemHousePanel.SetActive(false);
        itemPetPanel.SetActive(false);
        itemPlayerPanel.SetActive(false);
        currentQuestsPanel.SetActive(false);
        allQuestsPanel.SetActive(false);
        Q001InfoPanel.SetActive(false);
        Q002InfoPanel.SetActive(false);
        Q003InfoPanel.SetActive(false);
        statPanel.SetActive(true);
    }

    public void ResumeGame()
    {
        pauseMenu.SetActive(false);
        pressButtonSound.Play();
        pauseMenuMusic.Pause();
        mainGameMusic.UnPause();
        Cursor.visible = true;
        pauseGameMenu = false;
        Time.timeScale = 1;
        thePlayer.GetComponent<CubeMovement>().enabled = true;
        playerNameDisplay.SetActive(true);
        savePositionButton.SetActive(true);
        loadPositionButton.SetActive(true);
    }

    public void RestartLevel()
    {
        pauseMenu.SetActive(false);
        pressButtonSound.Play();
        Cursor.visible = true;
        pauseGameMenu = false;
        Time.timeScale = 1;
        thePlayer.GetComponent<CubeMovement>().enabled = true;
        playerNameDisplay.SetActive(true);
        savePositionButton.SetActive(true);
        loadPositionButton.SetActive(true);
        SceneManager.LoadScene(1);
    }

    public void QuitToMenu()
    {
        pauseMenuMusic.Pause();
        pauseMenu.SetActive(false);
        pressButtonSound.Play();
        Cursor.visible = true;
        pauseGameMenu = false;
        Time.timeScale = 1;
        thePlayer.GetComponent<CubeMovement>().enabled = true;
        playerNameDisplay.SetActive(true);
        savePositionButton.SetActive(true);
        loadPositionButton.SetActive(true);
        SceneManager.LoadScene(0);
    }
}

The way I do it, add a “canvas group” component to the parent canvas.

Then in your script change the alpha to 0.

GetComponent<CanvasGroup>().alpha=0;

If you want to fade, a function similar to this can help:

Private ienumerator FadeMeOut() { CanvasGroup cg = GetComponent(); while (cg.alpha>0) { cg.alpa-=Time.deltatime; } return null; }

I just typed this on my mobile, so haven’t tested/debugged it, but this should give you enough to work on.

How it helps.

Kev