Multiple functions in a single button not working

I’m attempting to make a build menu, RTS style, in which several buttons with graphics appear when you right-click, then you click one of those buttons, it prints the button’s name, then turns the build menu off.

So far I have gotten the build menu to turn on with right click, show the buttons in their proper places, assign textures to those buttons, have the build menu turn off when I right click, make the window dragable… but when I click one of the buttons nothing happens.

What exactly am I doing wrong here? The button code worked just fine until I put buildMenuOn = false; in there.

(Yes, this is designed to have 2 rows of 4 buttons each. I haven’t yet made textures or assigned the other buttons yet, and only have 3 so far.)

#pragma strict

var buildMenuOn : boolean;
var buildRect : Rect = Rect (20,20,235,300);

// Textures for the buttons
var buttLivingQuarters : Texture;
var buttCafeteria : Texture;
var buttMedical : Texture;

function Start () 
{
var buildMenuOn = false; // the build menu starts out off
}

function Update () 
{
	if (Input.GetButtonDown("Fire2")) // if right mouse button is clicked
	{
		if(buildMenuOn) // if the build menu is visible
		{
			buildMenuOn = false; // turn it off
		}
		else // if the build menu is not on
		{
			OnGUI(); // bring up the build menu GUI
			buildMenuOn = true; // turn the build menu on
		}
	}
}



function OnGUI()
{
	if(buildMenuOn)
	{
			buildRect = GUI.Window(0,buildRect,BuildWindow,"Please select your Prefab");
	}
}

function BuildWindow(windowID: int)
{
		// Draggable Windows
		GUI.DragWindow(Rect(0,0,10000,10000));
		
		//build button controls
		
		// top line
		if (GUI.Button(Rect(10,20,50,50),buttLivingQuarters))
		{
			print("Living Quarters");
			buildMenuOn = false;
		}
		
		if (GUI.Button(Rect(65,20,50,50),buttMedical))
		{
			print("Medical");
			buildMenuOn = false;
		}
		
		if (GUI.Button(Rect(120,20,50,50),buttLivingQuarters))
		{
			print("Living Quarters");
			buildMenuOn = false;
		}
		
		if (GUI.Button(Rect(175,20,50,50),buttLivingQuarters))
		{
			print("Living Quarters");
			buildMenuOn = false;
		}
		
		
		//second line
		if (GUI.Button(Rect(10,75,50,50),buttCafeteria))
		{
			print("Living Quarters");
			buildMenuOn = false;
		}
		
		if (GUI.Button(Rect(65,75,50,50),buttCafeteria))
		{
			print("Living Quarters");
			buildMenuOn = false;
		}
		
		if (GUI.Button(Rect(120,75,50,50),buttCafeteria))
		{
			print("Living Quarters");
			buildMenuOn = false;
		}
		
		if (GUI.Button(Rect(175,75,50,50),buttCafeteria))
		{
			print("Living Quarters");
			buildMenuOn = false;
		}
}

Please move this line

GUI.DragWindow(Rect(0,0,10000,10000));

to the end of OnGUI method. This should fix it.

Additionally, you don’t have to explicitly call OnGUI (and besides, currently calling it does nothing, as it is called when buildMenuOn is still false). So I suggest changing the code of your right mouse button test to:

if (Input.GetButtonDown("Fire2")) // if right mouse button is clicked
{
    buildMenuOn = !buildMenuOn;
}