- Home /
Appearence of the GUI element after a certain action
I need a functor inside my OnGUI() function which draws a GUI.Button after an input.
So I try to do this
void OnGUI()
{
System.Action Some = () =>
{
if (Input.GetKeyDown(KeyCode.Q))
GUI.Button(new Rect(0, 0, 100, 20), "Done");
};
Some();
}
Nothing happens but when I use that
void OnGUI()
{
System.Action Some = () =>
{
GUI.Button(new Rect(0, 0, 100, 20), "Done");
};
Some();
}
GUI.Button appears, so the functor works well. But it doesn't fit my needs. So I figured out that problem doing brute-force
bool isSome = false;
void OnGUI()
{
System.Action Some = () =>
{
if (Input.GetKeyDown(KeyCode.Q))
isSome = true;
if(isSome)
GUI.Button(new Rect(0, 0, 100, 20), "Done");
};
Some();
}
It seems to me not good at all.
Can someone explain me why doesn't GUI element appear after an action (without bool flag)?
Thanks in advance!
Answer by benni05 · Jun 30, 2011 at 08:39 PM
The OnGUI is called once per frame and DOES render your button even in your first code example IF you hit the key. BUT you can't see it really because it is displayed just during that particular frame of the key hit. That's why in your second example where you omitted the keypress it is displayed all the time. In other words, the GUI call that renders the button is not an on/off thing but needs to be called every frame in case you want to display the element.
Ben
Your answer
![](https://koobas.hobune.stream/wayback/20220612130348im_/https://answers.unity.com/themes/thub/images/avi.jpg)