- Home /
Disable and enable canvas group by pressing an UI button. (C#)
Hello everyone. I'm new to Unity and don't know how to script (C#) properly.
What i want to do id to show a canvas group by pressing one single button. I have a UI button set up that should enable a canvas group (Alpha, Interactable, Blocks Raycast) if pressed once and to hide again if pressed another time. Is that possible in Unity?
Answer by DarthHawk13 · Dec 28, 2016 at 05:29 AM
I created a tutorial video that answers your question. The button doesn't toggle however. I'll need to update the tutorial. :)
Answer by jprocha101 · Jan 03, 2016 at 08:40 PM
You will need to:
Create a script (see code below)
Attach to a gameobject in your scene
Assign the script to the OnClick event listener of your button
Select the method from the script for the listener
The code to toggle whether canvas group is displayed or not would look similar to this:
// This method will be set in the inspector in the OnClick section of your button
public void ToggleCanvasGroupActive()
{
// This will set the canvas group to active if it is inactive OR set it to inactive if it is active
MyCanvasGroup.gameObject.SetActive(!MyCanvasGroup.gameObject.activeSelf);
}
I suggest you watch the official Unity tutorials because this is all covered there in detail. There are tutorials on scripting as well as how to handle the UI elements.
See this video for a detailed description of the UI button functionality.
See this video on activating gameobjects.
Thank you! I'll definitely check out those tutorials!
Downvoted because question is about enabling/disabling Canvas Group , your answer is about disabling a canvas itself.
I changed it say canvas group. Just switched out the variable name, same code.
Also, what is $$anonymous$$yCanvasGroup
here? (or $$anonymous$$yGroupCanvas
, presumably a typo for the same thing)
I think line 5 should just be
gameObject.SetActive(!gameObject.activeSelf);
And shouldn't step 3 say "assign the ToggleCanvasActive method in the new component to the OnClick event listener...", as per the comment in the code? (You don't assign a "script" to an event).
amanpu's issue regarding Canvas vs CanvasGroup is valid but now fixed. Ultimately this isn't a bad thing in the context of the question, since there's nothing special about Canvases or CanvasGroups in this context - using a button to toggle a GameObject's active status works the same way regardless of what components are on that GameObject.
As @amanpu said, the question is about enabling / disabling the CanvasGroup component, not about activating / deactivating the CanvasGroup's game object. Unfortunately, CanvasGroup doesn't seem to have a CanvasGroup.enabled flag, unlike most components.
It was never clear to me what the OP was after. Yes the title mentions enabling and disabling a canvas group, but in the question they're also talking about "hiding" it. And if you look at the tutorial linked to in the other answer (which the OP said was helpful). it's just about making canvas groups invisible/non-interactable by changing their values. So I suspect the question is not about enabling/disabling in the sense you mean at all.
Anyway, what exactly is it you're trying to achieve? If you want to turn a canvas group component off, so that it's not having any effect on the ui elements that it's controlling, can't you just set the canvas group's fields accordingly? e.g set its alpha to 1 and so on.