How do I destroy a GUI button when I click on it

I am making a upgrade menu. All I want to know is how do I destroy a GUI button when I click on it. So when I buy a spell the button gets destroyed so that I cant buy it again.

Maintain some form of state whether or not it has been click. And if so, simply don't draw it again in the Update.

1 Answer

1

Well it depends on how your project is coded. But generally I don’t destroy any GUI buttons I would just hide it. So in your case I’d probably use some if(condition) to show the button, and once you click it that condition no longer applies. And then you would need some code to reconfigure your menu if there’s more buttons that are supposed to take it’s place.

It could be a very simple or somewhat complex solution depending on exactly what you are trying to do.

So I guess I’ll add a brief example in case it helps:

public Texture2D healthPotionTexture;

public GUIStyle GUIStyleButton;

bool buttonPressedBool = false;

void OnGUI()
{

  if (buttonPressedBool == false)
  {
    //healthPotionTexture is the name of the texture used to display button (can be .PNG for example)

    //GUIStyleButton will make the button appear without any borders

    if (GUI.Button (new Rect (vectorPosition.x, vectorPosition.y, vectorSize.x, vectorSize.y), healthPotionTexture, GUIStyleButton))
    {
    //what the button does...

       buttonPressedBool = true;
    }
 }
}