How to stop a GUI (JavaScript)

My code is simple, it consists of a Debug Mode and a variable telling the code if the inventory is active or not. When you press e it makes the inventory active and therefore draws the inventory, when the inventory is active and you hit e again it announces that you have shut the inventory but it does not go away. SO the script is working perfectly fine exept the fact that after I hit e a second time the inventory is still on my screen (though the console does announce the debug mode). Please help!

/* Public Variables */
public var DebugMode : boolean = false;

/* Private variables */
private var inventoryActive : boolean = false;

function Start () {

}

function Update () {

if(Input.GetKeyDown(“e”)) { /* If press e, make inventoryActive true /
inventoryActive = true;
if(DebugMode == true) {
Debug.Log(“Opening Inventory”); /
If debug mode is on, announce that the user is attempting to open their inventory */
}
}

}

function OnGUI () {
/* Inventory /
if(inventoryActive == true) { /
If inventory is open /
GUI.Box (Rect (0,0,600,800), "Items: "); /
Draw the inventory window */

if(DebugMode == true) { /* If debug mode is on, announce that inventory has been opened /
Debug.Log(“Inventory Opened”);
}
if(Input.GetKeyDown(“e”)) { /
If inventory is active, and you press e, close the inventory window /
inventoryActive = false;
if(DebugMode == true) {
Debug.Log(“Closing Inventory”); /
If debug mode is on, announce that user is attempting to close their inventory */
}
}
}
}

you’re telling the update function to always change the value of inventoryActive to true anytime you press e… so changing the code to something like this would fix it:
instead of this:

use this:

if(Input.GetKeyDown(“e”)) { /* If press e, make inventoryActive true */
inventoryActive = !inventoryActive;

Well I guess a better way to fix that would be just to define that it has to be false when I press e. Also the script you gave me doesn’t help me close the GUI. Changing the value to false has nothing telling it to undraw the GUI. And I don’t know how to do that.

/* Public Variables */
public var DebugMode : boolean = false;

/* Private variables */
private var inventoryActive : boolean = false;

function Start () {

}

function Update () {

if(inventoryActive == false) {
if(Input.GetKeyDown(“e”)) { /* If press e, make inventoryActive true /
inventoryActive = true;
if(DebugMode == true) {
Debug.Log(“Opening Inventory”); /
If debug mode is on, announce that the user is attempting to open their inventory */
}
}
}
}

function OnGUI () {
/* Inventory /
if(inventoryActive == true) { /
If inventory is open /
GUI.Box (Rect (0,0,600,800), "Items: "); /
Draw the inventory window */

if(DebugMode == true) { /* If debug mode is on, announce that inventory has been opened /
Debug.Log(“Inventory Opened”);
}
if(Input.GetKeyDown(“e”)) { /
If inventory is active, and you press e, close the inventory window /
inventoryActive = false;
if(DebugMode == true) {
Debug.Log(“Closing Inventory”); /
If debug mode is on, announce that user is attempting to close their inventory */
}
}
}
}

Bump

Bump -_- No answers? Someone has to know how to make a GUI go away!

A: Stop bumping every hour or so.
B: Learn to use the code tags.
C: You don’t make Unity GUI go away. If you have your code set up correctly it automatically goes away. If it doesn’t go away, there is something wrong with your code.

I can see clearly that you are telling to hide and show the gui in TWO places: OnGUI and Update. You only need one.

This is what is happening from what I understand looking at your code:

Press “e”
Frame 1 hide gui (function Update)
Frame 1 (same frame) show gui (function OnGUI)
Frame 2 //Gui is still there… but it DID hide… just ran the boolean in the same frame.

Next issue.

You need to make a toggle, looks like once you press e, you can`t get back to open.

So first, solve the duplicate boolean calls.

Second, make the if(Input.GetKeyDown(“e”)) actually TOGGLE the boolean. (there are a couple ways to do, just degrees of cleanliness).

It would be better to let you hear the solution and try to sort it out. Might help you remember.

Good luck and post your answer.

var debugMode:boolean = true;
private var isOpen:boolean = false;

function Update() {
  if (Input.GetKeyDown("e")) isOpen = !isOpen;
  if (debugMode) Debug.Log("Do what you want here ",this);
}
function OnGUI() {
	if (isOpen) {
		// Draw something here.
	}
}

pretty sure this works…dude i created my own GUI solution…and @Lamont is right…you have done terrible