- Home /
On Button Press, Show GUI Window
I'm making a horror RPG game and I am currently working on the NPC's. I have the characters and animations done I now need to be able to interact with the NPC's. Right now I have a script that shows a button that says "Talk to Grandpa Joe". This button is only shown when the play activates the trigger that is surrounding Grandpa Joe. Once the character leaves the trigger area, the button disappears.
Now I need help. I've searched Google, Bing, and Yahoo trying to find out how I can make a GUI Window appear once I press the "Talk to Grandpa Joe" button. This window is meant for dialogue yet I can't get a GUI window to appear on button press. Can anyone please help me?
Here's a copy of the script I'm using:
[CODE]
var toggleGUI : boolean;
function OnTriggerEnter (other : Collider) {
toggleGUI = true;
}
function OnTriggerExit (other : Collider) {
toggleGUI = false;
}
function OnGUI () {
if (toggleGUI == true)
GUI.Button (Rect (450,450,200,30), "Talk to Grandpa Joe");
}
[/CODE]
Answer by hamcav · Aug 09, 2013 at 09:57 PM
....Try this ....
var toggleWindow :bool = false;
function OnGUI () {
if (toggleGUI == true){
if(GUI.Button (Rect (450,450,200,30), "Talk to Grandpa Joe"))
toggleWindow = true;
}
if (toggleWindow == true)
windowRect = GUILayout.Window (0, windowRect, 0, "My Window");
}
tell me if any problems occur
You don't know how happy I am! You helped me out a ton. I modified the code to have a GUI.Box than a window. But thanks so much!
Answer by zombience · Aug 09, 2013 at 09:43 PM
put the GUI.Button inside an if statement. seems somewhat counterintuitive, but it's how it works.
so, putting the full code for the GUI button in will render the button, and having it inside an IF statement will receive whether it has been pressed or not.
Check out the documentation: http://docs.unity3d.com/Documentation/ScriptReference/GUI.Button.html
if (GUI.Button(Rect(10,10,50,50),btnTexture))
Debug.Log("Clicked the button with an image");