- Home /
How to remove a GUI element when button is pressed C#
is there a way to remove a GUI element when a button is pressed?
if(GUI.button(new rect(0,0,25, 25), "Button that needs to be removed")){ //Remove button that needs to be removed }
Comment
Best Answer
Answer by Bunny83 · Jun 17, 2012 at 02:07 AM
Unity uses an immediate-mode GUI system. The GUI is drawn every frame. If you want to hide an element, don't draw it. for example:
bool hasBeenPressed = false;
void OnGUI()
{
if (!hasBeenPressed)
{
if(GUI.button(new rect(0,0,25, 25), "Button that needs to be removed"))
{
hasBeenPressed = true;
}
}
}
If you want to hide the whole GUI in this script (and of course the script doesn't have other functions), you can also disable the script:
void OnGUI()
{
if(GUI.button(new rect(0,0,25, 25), "Button that needs to be removed"))
{
enabled = false;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613072858im_/https://answers.unity.com/themes/thub/images/avi.jpg)