How to disable a menu with a menu option?

In our menu we want to have a “cancel” button which would close the menu. Our code for this is:

GUI.SetNextControlName(buttons[4]);
		if (GUI.Button (new Rect (0, 400, 100, 100), buttons [4])) {

			//when selected Cancel button
			if (Input.GetButtonDown ("Select")) {
				print ("It Works");
				GameObject.Find("cursor").GetComponent<Menu> ().enabled = false;
			
			}
		}

We have had luck so far, making the menu pop up and disappear by using GetComponent to just enable and disable the script. This is probably a really bad way to do this, but we are new and it makes sense to us.

So with this code, we are trying to make the script disable itself if the button is clicked, basically. Where the GetComponent usually works elsewhere, nothing happens when implemented here for some reason.

We put in
print (“It Works”);
to see if the the conditional statement was the problem, and it was not the problem.

So we want to know if you simply cannot disable a script from inside itself.

Alternatively, if all we have done is shamefully poorly done, might there be a better way to make a menu pop up and go away?

I would suggest using a boolean to flag whether or not to show the GUI menu.

public bool ShowMenu = false;

void OnGUI()
{
	if (ShowMenu)
	{
		// Put other GUI code in here!
		// Here is your button code afterwards:
		if (GUI.Button(new Rect(0, 400, 100, 100), buttons[4]))
		{
			if (Input.GetButtonDown("Select"))
			{
				Debug.Log("It works!");
				ShowMenu = false;
			}
		}
	}
}

However, you guys should not be using Unity’s Legacy GUI system, and instead use the new Canvas UI system.