Explain me GUI button creation

Hello,

What have i made
I have build a HUD which has a for loop which creates 7 buttons on the HUD. The buttons have a picture of an item in them. When I press on of the buttons. The background of the hud is placed outside the screen. This makes it seem outside the screen.

What am i trying to make
When I press the right mouse button, the hud appears. When I then click one of the buttons, they, and the hud disappears.

Problem
I have no idea how to access the buttons since i have not created them through an instance. Or anything. I till today didn’t even know you could create stuff within an if statement.

If someone could explain what I am doing wrong. Or what I should do to make it possible to access the buttons properties. Please do.

My code:

public var visibility:boolean;
//previewObjects which holds all object preview textures
public var  previewObjects:Texture2D[];
private var previewObjectPositions:Rect[];
//objectPreviewSize is the Width and the Height og the preview button on the GUI
public  var objectPreviewSize:Vector2;
//contentStartingPosition is the position of the GUI inventory+startposition
public var contentStartingPosition:Vector2;
//contentOffsetPosition is a variable, added to all the previewbutton's position
public var contentOffsetPosition:Vector2;
//contentSeparation is the width between the preview objects
public var contentSeparation:float;

function OnGUI() {
	for(var i:int = 0; i < previewObjects.length; i++)
	{
		if (GUI.Button(Rect(contentStartingPosition.x+(objectPreviewSize.x+contentSeparation)*i+contentOffsetPosition.x, contentStartingPosition.y+contentOffsetPosition.y, objectPreviewSize.x, objectPreviewSize.y), previewObjects[i]))
		{
			GameObject.Find("GUI").GetComponent("GuiInventoryVisibility").hide();
			GameObject.Find("Main Camera").GetComponent("CharacterMotor").canControl = true;
			GameObject.Find("Main Camera").GetComponent(MouseLook).Enable();
		}
	}

I took a look at your code and read your statement and I’m a bit confused as to what you are trying to do. Can you make a quick (photoshop?) mockup of the HUD you’d like?

Working the other way, this is some (Unity 2.6) code that works for me:

function OnGUI () {

	if (guiSkin)
		GUI.skin = guiSkin;
	else
		Debug.Log ("StartMenuGUI: GUI Skin object is missing!");

	var backgroundStyle : GUIStyle = new GUIStyle ();

	switch (menuLevel) {
		//	New StartMenuLevel.Start with all games on the front screen.
		case StartMenuLevel.Start:
			if (menuLevelChanged) {
				currentBadge = GetCurrentBadge ();
				menuLevelChanged = false;
			}

			//	Top Banner
			GUI.DrawTexture (Rect (0, 0, 320, 85), tinyArcadeLogo);

			
			if (GUI.Button (Rect (0, 105, 320, 66), "Space Duel", "TA Type")) {
				menuLevelChanged = true;
				menuLevel = StartMenuLevel.SpaceDuel;
			}
			
			if (GUI.Button (Rect (0, 160, 320, 66), "Volley", "TA Type")) {
				menuLevelChanged = true;
				menuLevel = StartMenuLevel.Volley;
			}
			
			if (GUI.Button (Rect (0, 215, 320, 66), "Survival", "TA Type")) {
				
				if (currentShipSurvival == "Y") {
					shipImageY.SetActiveRecursively(true);
				}
				if (currentShipSurvival == "V") {
					shipImageV.SetActiveRecursively(true);
				}
				menuLevelChanged = true;
				menuLevel = StartMenuLevel.Survival;
			}
			
			
			if (GUI.Button (Rect (0, 315, 320, 66), "Options \nInstructions", "TA Type")) {
				menuLevelChanged = true;
				menuLevel = StartMenuLevel.StartOptions;
			}

			//	Bottom Banner
			GUI.DrawTexture (Rect (0, 395, 320, 85), currentBadge);
			
		break;

With OnGUI, you don’t really access the buttons the way you would game objects in your game (if that’s what you are trying to do…), you just make them on the fly with each loop of OnGUI.

or did I misunderstand?? Maybe?

ps: the 3d platformer tutorial is a good place to look at setting up OnGUI buttons and screens.