- Home /
C# GUI Destroy or Disable?
Hi everyone, is there a function that destroys or disables a GUI? I need a way to disable or destroy GUI.Button when the boolean isAvailable is false.
bool isAvailable;
if( isAvailable = true){
if (GUI.Button(new Rect(100, 10, 50, 50), "SomeButton")){}
}
else if( isAvailable = false){
//Destroy or Disable GUI.Button
}
I would like to answer this in a way that best suits your needs. Could you please answer the following for clarification:
Do you want the button to be disabled (on screen but can't be clicked)?
Do you want the button to disappear completely (not on screen at all)?
Do you need to disable/enable the button from other scripts without having a reference to the object the GUI code is on (static reference)?
I want the button to disappear completely (not on screen at all).
Answer by TrickyHandz · Aug 28, 2013 at 09:55 PM
You were almost there and didn't know it. You don't have to worry about destroying a button at all, just put a condition on whether or not the button should be drawn in the first place. Remember that buttons are redrawn every time OnGUI is called.
Here is a quick example of how to stop drawing the button after it is clicked:
C#
using UnityEngine;
using System.Collections;
public class ExampleConditionalButton: MonoBehaviour
{
public bool isAvailable = true;
void OnGUI()
{
// Check to see if this button
// should be available
if (isAvailable)
{
// Draw the button
if (GUI.Button(new Rect(100, 10, 50, 50), "SomeButton"))
{
// Disable Button After Click
isAvailable = false;
// Add code for any additional
// behavior when the button is clicked
}
}
}
}
Hope that helps you out.