Window to pop up when you press a key

Alright I want to get a GUI.Window to appear when I press C so right now my code is

var TheWindow = Rect (20,20,120,50);

Function OnGUI(){
   if (Input.GetKeyDown ("c")){
         Screen.lockCursor = false;
	 TheWindow = GUI.Window(0,TheWindow,DoTheWindow, "Image");
   }
}

function DoTheWindow(windowID : int){
   if (GUI.Button (Rect (10,20,100,20), Image)){
      print("It Works");
   }
}

I don’t get any errors when I run it so I assume it has to do with the GetKeyDown statement because when I press C it doesn’t do anything but unlock my cursor

any suggestions, thanks

The problem here is that you’re only looking for the key press in the OnGUI call which is actually called multiple times per frame. The first time it runs it sees the key has been pressed, unlocks the cursor (which is a persistent change) and draws the Window. But then it runs again the next frame and guess what? The key press event is flushed and that window goes away. Move your key press detection to an Update function, do your unlocking and set a variable (boolean) to trigger window drawing. Like this:

var showWindow = false;

function Update () {
  if (Input.GetKeyDown("c")) {
    showWindow = true;
    Screen.lockCursor = false;
  }
}

function OnGUI () {
  if (showWindow) {
    TheWindow = GUI.Window(...);
  }
}

// etc...

Doing it that way will allow you to have a persistent “keep the window open” behavior. Remember, you need that GUI.Window(…) call to be active and called every frame you want that window displayed, it’s not a “call it once and you’re done” system. :slight_smile:

Oh… Now I understand :P, thanks again Higgy