How to activate or deactivate a certain Canvas by unsing script (C#)?

Hey I am making a mobile game and ich have a PauseButton which should activate a PauseMenu (actually a Canvas). I just tried to use this script but it doest work. (btw. yes i used an EmptyGameObject for the script and the onClick component on the button)

using UnityEngine;
using System.Collections;

public class PauseMenuScript : MonoBehaviour
{

    public void ToggleCanvasGroupActive()
    {
        // This will set the canvas group to active if it is inactive OR set it to inactive if it is active
        PauseMenu.gameObject.SetActive(!PauseMenu.gameObject.activeSelf);
        {

        }

    }
}

if you have an answer to the question it would be nice if you would show me the entire function because else i wont understand it because i´m a nooby :smile:
thank you very much for every answer

  • You didn’t have to type brackets after SetActive. Remove them.
  • How do you get PauseMenu variable? Is it static, singleton? I just do not see that you declared it, at least here.

well i didnt get the PauseMenu variable, what should i write as the variable?

The first option:

public GameObject PauseMenu;

For the second option, you can do that

    public void ToggleCanvasGroupActive(GameObject PauseMenu) // <- Change is here
    {
        PauseMenu.gameObject.SetActive(!PauseMenu.gameObject.activeSelf);      
    }

And when you will setup onClick settings of button in the inspector, you will need to assign menu canvas in the field that will appear there.