0
\$\begingroup\$

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.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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.

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

your sample looks like the code in Unity's docs http://unity3d.com/support/documentation/ScriptReference/GUI.Window.html 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?

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – lathomas64
    Jan 23, 2012 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .