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

I am trying to do some GUI work with unity but am having some issues.

I call a window with this code:

fortuneRect = GUI.Window(0, fortuneRect, fortuneWindow, "Your future");

and inside the window I have a button

if(GUI.Button(new Rect(10, 10, 150, 20), "save fortune")){
       Debug.Log("save fortune button press");
       writeToFile("Button pressed!", "fortune.txt");
       Debug.Log("After save fortune button press");

but the button doesn't fire any of its events on click. I tried commenting out the writeToFile but even with only Debugs in the body it doesn't fire.

share|improve this question
up vote 2 down vote accepted

You need to have the button code inside

void OnGUI() {...}

Provided you ignore the window part, looks fine to me: in other words, the button code on its own, without using a window, should work. Once you have the button code working, go from there. Try doing exactly the same in a brand new project, if all else fails. You should find that alone works, and something else in your current project is preventing proper compilation or runtime operation.

share|improve this answer
the button code is inside fortuneWindow(int windowID) which is called from OnGUI() – lathomas64 Jan 23 '12 at 18:56
Did you try with a fresh project and minimal code? Minimal working test case is the best way to get things operational. – Arcane Engineer Jan 23 '12 at 19:20
Apparently GUI.DragWindow stops execution. – lathomas64 Jan 25 '12 at 0:44
@Iathomas64 Did you solve the issue following this? If my answer led you to some higher understanding, feel free to accept it. – Arcane Engineer Jan 26 '12 at 20:29

your sample looks like the code in Unity's docs so at first glance should be fine. Can you provide more info?

The first arg in your call to GUI.Window() should be the window id. Do you mean for that to be zero? Also, I assume your GUI.Button() call is inside the 'fortuneWindow' callback function. What does the rest of that function look like? You're not inside an if (windowId != 0) block or #ifdef UNITY_EDITOR or anything? Do you have overlapping windows that might be stealing each other's focus?

What's your runtime environment? Is it a custom editor window or a game UI? Are you seeing this behavior when running in the editor or one of your build targets?

share|improve this answer
I can provide more detail tonight when I'm back at home but for now. Yes I mean for that to be 0, its the only window I'm using although I tried changing the value with no success. My fortuneWindow method doesn't reference the windowID at all the GUI.Button() call isn't inside any control structures at all. I took these out but I tried putting debug statements before and after the button if block and both fired, its just the insides of the IF that never fired. – lathomas64 Jan 23 '12 at 18:55

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.