jni97
June 22, 2015, 3:40pm
1
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).
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.
Attach CanvasGroup component to UI. 
Create script and add to Canvas or another element.
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.