How to hide Canvas?

Hi,
I made an Inventory System.
The problem is that if I write “Canvas.SetActive(false);”, I can’t use the Script on it.
But I have to use the attached script.

I think instead of setting the SetActive state to false, I have to change the alpha channel of the canvas.

How can I change this?(I’m using the new UI)

Thanks for help!

If I understand you correctly you have a canvas in which you display your inventory and you want to be able to hide/show this inventory.

To be able to achieve this it might be worth adding your inventory objects to an empty gameobject which you name Inventory (see pic).

48744-canvas.jpg

You can then toggle this GameObject via SetActive()

Example (whereas Inventory is a the empty gameobject where your inventory items reside in):

        private void ToggleInventory()
        {
            Inventory.SetActive(!Inventory.activeSelf);
        }

I have the same problem in 2022 ^^

Hi! Use CanvasGroup Alpha property.

  1. Attach CanvasGroup component to UI. ![199085-canvas-group.png|399x137](upload://gtSo7xzD9vT1UNhY2eAUwZSMor2.png)
  2. Create script and add to Canvas or another element.![199086-inventoryscript.png|400x92](upload://5dwCXbO28G0qTqg9OCwUpsKtbK0.png)
  3. Transmitt CanvasGroup element to script field
    public class Inventory : MonoBehaviour
    {
        [SerializeField] private CanvasGroup _canvasGroup;
    
        void Start()
        {
            SetVisible(false);
        }
    
        public void SetVisible(bool isVisible) {
            _canvasGroup.alpha = isVisible ? 1 : 0;
            _canvasGroup.interactable = isVisible;
            _canvasGroup.blocksRaycasts = isVisible;
        }
    

    Or get it from current element if you set script to element with CanvasGroup

    private CanvasGroup canvasGroup;
    
        private void Awake()
        {
            canvasGroup = GetComponent<CanvasGroup>();
        }
    

    But don’t forget to add [RequireComponent(typeof(CanvasGroup))] behind class, if you will do that.