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
}

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;
    }
}