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