Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On unity3d website in the manual on this page:

I found an example of "GUI.changed" function usage(below). And it looks like it creates toolbar object on every frame. Am I right or not?

/* GUI.changed example */

// JavaScript
private var selectedToolbar : int = 0;
private var toolbarStrings = ["One", "Two"];

function OnGUI () {
    // Determine which button is active, whether it was clicked this frame or not
    selectedToolbar = GUI.Toolbar (Rect (50, 10, Screen.width - 100, 30), selectedToolbar, toolbarStrings);

    // If the user clicked a new Toolbar button this frame, we'll process their input
    if (GUI.changed)
        print ("The toolbar was clicked");

        if (selectedToolbar == 0)
            print ("First button was clicked");
            print ("Second button was clicked");
share|improve this question
the function title is onGUI, not onUpdate, or onRender or similar other things. so I can assume it's not creating GUI in every frame but only when one enters GUI enabled scene. – Ali.S Mar 7 '12 at 22:13
It looks really strange. Because in the same block of code they create control and immediately check it's status. Will it be checked on every frame or only one time in the example? – st0ne_c0ld Mar 7 '12 at 22:19
up vote 6 down vote accepted

OnGUI functions are called multiple times a frame. It's called once to do setup code for doing GUILayout stuff. It's called again to actually draw the frames, and it's called N additional times for (I think) each input event that might be relevant to the UI.

This is known as "Immediate Mode GUI", which you can read a bit more about here: (as well as other places if you look up that phrase, or "IMGUI").

OnGUI is called for rendering and handling GUI events.

This means that your OnGUI implementation might be called several times per frame (one call per event). For more information on GUI events see the Event reference. If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.