- Home /
The question is answered, right answer was accepted
Close UI using Canvas Group
Issue:
I'm working with UI in my game (Don't Die, name will probabbly change). I need to be able to open and close menus in some specific situations. "So," - I thought - "why not to make a universal script using Canvas Group? It will save me from a lot of headaches!"...
I have only one problem. With my script, I'm able to open the... Inventory, let's say. But, unless I use a close UI.Button for it, I'm not able to close the "Inventory" by pressing any key on the keyboard.
How do I do it:
When I'm closing I set CanvasGroup.alpha to zero, so the menu gets invisible, CanvasGroup.blockRaycasts to false, so it don't block other menus and CanvasGroup.interactable to false, so the player can't click in anything on it.
To open I just do the same thing, but in the oposite direction.
But if I open the menu it doesn't close again. Unless I make a close button.
What I've already tried?
I've tried to make Open() and Close() IEnumerators, instead of voids, but with no success.
Writing the whole open/close code inside the ifs doesn't work as well.
Removing the Draggable-related pieces.
Other stuff I don't remember by now. (Way too many things...)
The Script:
https://0bin.net/paste/ZlOsi+ydMBlBgMNP#-hO01knc97WAOmJLT3pqhkbYtqGnFt5bdKjIt8RylOE
Conclusion:
I don't get any exeptions, any warnings or anything about this. And I haven't found an answer on Google as well. If someone could figure out why this doesn't work I would be gratefull. Thanks.
It's a shame that Unity Answers doesn't even have a topic/tag about it.
Answer by ShadyProductions · Aug 10, 2017 at 12:37 PM
In your startclosed you set state to false if start closed is true
then when you press the button you do
state = !state;
so state becomes true;
then when state is true you call Close();
and in close you set state to false
So next time you click, state wil again be turned true and call close again.
You're stuck in that loop there. Thats your issue.
So instead of in your open() and close()
Edit: remove state = !state; from the update and it should work.
Okay, I'll try ro fix this once I get to my computer. Thanks.