showing/hiding gui

Hey all,

i am very new to unity and programming, and i want to show or a hide a gui by pressing escape
this is the code i have, but the gui wont show or hide when i press escape.
i am not sure what i am doing wrong, i already have connected the scripts by adding a empty game object and adding the scripts to the camera and object.

Any help would be appreciated.

var menuGui : MenuGui;

function update ()
{
if (Input.GetKeyDown("escape"))
{
if (menuGui.enabled == true)
{
menuGui.enabled = false;
}
else
{
menuGui.enabled = true;
}
}
}

You may try that. The changes are bold. Update should be upper case and using KeyCode.Escape avoids potential issues as well.

var menuGui : MenuGui;

function [B]U[/B]pdate ()
{
	if (Input.GetKeyDown([B]KeyCode.Escape[/B]))
	{
		if (menuGui.enabled == true)
		{
			menuGui.enabled = false;
		}
		else
		{
			menuGui.enabled = true;
		}
	}
}

Warning: UNTESTED!

awesome, thanks a bunch :smile: i thougt update was lowercase tho :stuck_out_tongue: stupid mistake.
But what potential issues are ye talking about?

If you are using strings as argument for GetKeyDown, it may happen that you include a typo and it still compiles fine. If you are using KeyCode.Escape and you make a type, you get the error message that you are using something that is not valid.

I would go a bit further and simplify it like this:

var menuGui : MenuGui;

function Update ()
{
	if (Input.GetKeyDown(KeyCode.Escape))
	{
		menuGui.enabled =!menuGui.enabled;
	}
}

thanks for the tips,

another thing tho,

var resumeButton : GUIStyle;
var menuBg : GUIStyle;

function OnGUI()
{
GUILayout.Button("", "resumeButton");
GUILayout.Box("", "menuBg");
}

now it keeps going in an endless loop when i press escape and stops when i press escape again.
i think it is because i am using OnGUI()
is there something i can replace that with to prevent this from happening?
also it wont draw the textures now and instead gives me this error:

Unable to find style ‘menuBg’ in skin ‘GameSkin’ KeyUp
UnityEngine.GUIStyle:op_Implicit(String)
MenuGui:OnGUI() (at Assets/Scripts/MenuGui.js:7)

I tried using function Start()
But then i get this:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.GUISkin.GetStyle (System.String styleName) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUIUtility.cs:1271)
UnityEngine.GUIStyle.op_Implicit (System.String str) (at C:/BuildAgent/work/6bc5f79e0a4296d6/Runtime/ExportGenerated/Editor/GUIUtility.cs:1052)
MenuGui.Start () (at Assets/Scripts/MenuGui.js:6)

problem with GuyStyle/Skin, look on unity scripting reference page at Layout, and Button…

Neither resumeButton nor menuBg are strings, so don’t use “”.

GUILayout.Button("", resumeButton);
GUILayout.Box("", menuBg);

right, sorry :S

anyway, i have edited the code again, and i have this now:

var resumeBtnTex : Texture;
var menuBgTex : Texture;
var menuBorderTex : Texture;

function OnGUI()
{
GUILayout.BeginArea (Rect (Screen.width / 2 - 128, Screen.height / 2 - 128, 512, 512));

GUILayout.Button (resumeBtnTex);
GUILayout.Box (menuBgTex);
GUILayout.Box (menuBorderTex);

GUILayout.EndArea();
}

problem is, it only displays the button with my texture, but then also with the unity default texture wrapped around it.
and it only shows the boxes as a long bar under the button.
how should i fix that?

EDIT:
i have edited the code a bit and given the boxes a fixed size, but still not displaying the texture:

var resumeBtnTex : Texture;
var menuBgTex : Texture;
var menuBorderTex : Texture;

function OnGUI()
{
GUILayout.BeginArea (Rect (Screen.width / 2 - 128, Screen.height / 2 - 128, 512, 512));

GUILayout.Button (resumeBtnTex , (GUILayout.Width(256)) , (GUILayout.Height(64)));
GUILayout.Box (menuBgTex , (GUILayout.Width(512)) , (GUILayout.Height(512)));
GUILayout.Box (menuBorderTex , (GUILayout.Width(512)) , (GUILayout.Height(512)));

GUILayout.EndArea();
}

EDIT2: oh, and i changed GUIStyle to Texture because with GUIStyle, it wouldnt display anything at all, even after assigning textures and stuff in the inspector.

EDIT3: so i have been editing the code AGAIN, and it sort of improved but still not right…
Now it displays the textures, but i dont know how to position them properly in this code:

var resumeBtn : GUIStyle;
var menuBg : GUIStyle;

function OnGUI()
{
GUILayout.BeginArea (Rect (Screen.width / 2 - 128, Screen.height / 2 - 128, 512, 512));

GUI.skin.button = resumeBtn;
GUILayout.Button(" ");
GUI.skin.box = menuBg;
GUILayout.Box(" ");

GUILayout.EndArea();
}