Clarification on Button/OnGUI

void OnGUI(){
if(GUI.button(new Rect(0, 0, 100, 40),“I am a button”)){
//do this do that
}
}

I am just wondering if the above code is creating a new button on every OnGUI call? or does it just create the button once and wait for it to be pressed?

Thanks in advance

Sunny

GUI.buttons only exist within the context of an OnGUI call - they don’t persist. So, in a way, yes that code is creating a new button every time OnGUI runs (which can be several times per frame), but that is how the inbuilt GUI system works.

Yes, but it’s just the internal workings of OnGUI(). Every frame, the GUI is cleared and redrawn depending on the several OnGUI() calls. But you don’t have to worry about memory filling up with buttons, because the old GUI from the previous frame is cleared. And from the user perspective the button is there constantly, it will not blink or something like that.