I am having problem with the button disabling, just want to know the proper manner to disable a GUIbutton with OnGUI function. As i used the this.active manner by making it false. Its creating mess with game object hit target.
example i kept the play button, so that if i hit it, it should fade or dissapear, so the way i done is the same in the unity site example
function OnGUI()
{
if(GUI.Button (Rect (160,100,150,100), “Play”))
{
print(“Started”);
plLevel = 1;
this.enabled = false;
}
}
this script is proper but i am adding this.enabled for button to dissapear and in this manner its creating the problem with the object hit target with its variable counter.
When you use this.enabled, you basically turn off the entire script.
So if you have code that should run (besides the OnGUI() function) - it will stop working, obviously.
If you just want to stop displaying the button, but still run other code just use if statements:
private var bButtonActive: boolean = true;
function OnGUI()
{
if (bButtonActive)
{
if (GUI.Button (Rect (160,100,150,100), "Play"))
{
print("Started");
plLevel = 1;
bButtonActive = false;
}
}
}
if you want to disable the button, but still show it (lets say, make it gray, but not clickable), you can just reverse the order of the if statements:
private var bButtonActive: boolean = true;
function OnGUI()
{
if (GUI.Button (Rect (160,100,150,100), "Play") bButtonActive)
{
print("Started");
plLevel = 1;
bButtonActive = false;
// Turn button grey...
}
}
yogesh,
Another option is to use the GUI.enabled option.
var GuiDisable = false;
function OnGUI()
{
if(GuiDisable)
GUI.enabled = false;
else
GUI.enabled = true;
if(GUI.Button (Rect (160,100,150,100), “Play”))
{
print(“Started”);
plLevel = 1;
GuiDisable = true;
}
// If you have more GUI elements after turn the GUI back on
GUI.enabled = true;
}
}
Thanks cyb3rmaniak and also jkevan. For guides and proper instructions.
Sweet! Didn’t even know it existed