- Home /
using c# script to open/close GUI window
Ive been tearing my hair out trying to solve this. Ok, lets admit the truth to the fact im a total rookie. This being said ill get to the point.
I am basically trying to get my inventory GUI window to open and close by pressing I.
My code looks like this :
using UnityEngine;
using System.Collections;
public class Inventory : MonoBehaviour
{
void OnGUI()
{
// Make a background box
GUI.Box(new Rect(10, 10, 100, 90), "Inventory");
}
private bool render = false;
private Rect windowRect = new Rect (20, 20, 120, 50);
public void ShowWindow() {
render = true;
}
public void HideWindow() {
render = false;
}
public void OnGUI() {
if (Input.GetKey(KeyCode.I))
ShowWindow();
if (Input.GetKey(KeyCode.I))
HideWindow();
}
}
doesnt work at all either lol. any help is greatly apprecited. Basically, I just am trying to have a GUI open up and close by pressing I. Thanks in advance for any help!
Answer by syclamoth · May 02, 2012 at 12:58 AM
What does 'render' do? From the looks of things, you're changing a variable that does nothing!
You have two 'OnGUI' functions declared. Don't do that. From the looks of things, the second one should be renamed 'Update' (since you're only doing things in there that should only be done in Update anyway), and then in your first OnGUI function try modifying it so that it actually checks the 'render' variable!
void OnGUI()
{
if(render)
{
// Make a background box
GUI.Box(new Rect(10, 10, 100, 90), "Inventory");
}
}
If you want the 'I' key to toggle the box, do this:
void ToggleWindow()
{
render = !render;
}
void Update()
{
if(Input.GetKeyDown(KeyCode.I))
{
ToggleWindow();
}
}
What if I just did something like this?
void OnGUI() { if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.I)) { // $$anonymous$$ake a background box GUI.Box(new Rect(10, 10, 100, 90), "Inventory"); } }
ANd something to close the window?
Don't use Input.anything inside of OnGUI. It's generally a bad idea. In any case, that will display the inventory when the key is held down, which isn't what you want. Just do it my way- there's a reason why I'm answering the question, and you're asking it.
Well now it looks something like this :
{
void OnGUI()
{
// $$anonymous$$ake a background box
GUI.Box(new Rect(10, 10, 100, 90), "Inventory");
}
void ToggleWindow()
{
render = !render;
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I))
{
ToggleWindow();
}
}
}
but its still having problems. obvious, i need more than a hand, so im going to have to just learn from the complete basics ins$$anonymous$$d. But for the sake of I spent 5 hours trying to do this simple thing, what IS the solution?
Are you even reading my answer? You need to make the OnGUI function acutally check the 'render' variable. Look back at my original answer, it's right there, in the first code box.
$$anonymous$$eep in $$anonymous$$d that OnGUI has no persistent state. It is always evaluated in terms of the current call- you don't 'add' and 'remove' elements, you either draw them or you don't. If you want an object to stay on the screen for several frames, you need to specifically draw that object in every frame's OnGUI function. The easiest way to switch between different 'modes' in an OnGUI is to use an if-statement to check variables on the object (which is persistant) to deter$$anonymous$$e what should be drawn in that specific frame.
Yes im really trying here lol. I by no means am a guru, i openly admit i am a complete rookie and furthermore stated I am in over my head and need to go back to square 1. I know it seems simple to people who have been in it for a while. Not TRYING to sound ignorant, I just do :|
well my code looks like
{ void OnGUI() { if (render) { // $$anonymous$$ake a background box GUI.Box(new Rect(10, 10, 100, 90), "Inventory"); } }
void ToggleWindow()
{
render = !render;
}
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.I))
{
ToggleWindow();
}
}
}
i thought the if (render) was checking the render? But the problem is i get the errors :
Error 2 The name 'render' does not exist in the current context
for everything named render. I tried moving a few things around to mess with it, but it just made matters worse so i went back to this code. Like I said I think i am just trying to bite off quite a bit more than I can chew currently. Thats my biggest problem lol.
Your answer
Follow this Question
Related Questions
Method is called, but GUI doesn't show up 1 Answer
Using an On-Screen GUI Button to show a GUI? 1 Answer
GUI not working correctly? (Double tooltip display)(c#) 1 Answer
How do I fix this null problem? 1 Answer
Distribute terrain in zones 3 Answers