# Unity 5 Closing Menu On Multiple GameObjects When A New Menu is Opened?

I am trying to make a menu system for each GameObject, and when a GameObject is clicked it will close all other menus and open the one for that GameObject. I am using one script for this:

using UnityEngine;
using System.Collections;

{
Vector3 point;

public Transform block;
public GameObject activeBlock;

bool buttonActive = false;
bool activeBlockActive = false;

string hitObject = "";

void Update()
{
point = Camera.main.WorldToScreenPoint(block.position);
}

void OnMouseDown()
{
hitObject = this.gameObject.transform.tag;

buttonActive = !buttonActive;
activeBlockActive = !activeBlockActive;

activeBlock.SetActive(activeBlockActive);
}

void OnGUI()
{
if(buttonActive)
{
switch(hitObject)
{
case "mine":
GUI.Box(new Rect(((point.x) * (1.08f)), ((Screen.height - point.y) * (0.1f)), (Screen.width * (0.3f)), (Screen.height * (0.5f))), "");
GUI.Button(new Rect(((point.x) * (1.08f)), ((Screen.height - point.y) * (0.87f)), (Screen.width * (0.05f)), (Screen.height * (0.06f))), "Build Mine");
break;
}
}
}
}

• What is your goal? What is your exact question/problem? – n_plum Feb 2 '17 at 17:47
• I want to make the script close every other GUI except for the one that was just opened. But I cannot figure out how to do so. – Tyler Feb 2 '17 at 17:50
• What do you mean by every other GUI? The inspector gui or some third party one? Do you have an example? – user3797758 Feb 2 '17 at 17:57
• By GUI I mean everything in the OnGUI() method – Tyler Feb 2 '17 at 18:05
• Why are you using the old gui system if this is unity5? – johnny 5 Feb 2 '17 at 21:37

This may work/help. The best solution I can muster/find is toggling the GUI objects individually and not just setting them all at once. You don't need to do it in a switch really. This is a mix of your code and the solution I found.

using UnityEngine;
using System.Collections;

{
Vector3 point;

public Transform block;
public GameObject activeBlock;

bool buttonActive = true;
bool activeBlock = false;

void Update()
{
point = Camera.main.WorldToScreenPoint(block.position);
}

void OnMouseDown()
{
buttonActive = !buttonActive; /* Turn button off */
activeBlock = !activeBlock; /* Turn block on */
}

void OnGUI()
{
if(buttonActive) /* Button is live */
{
GUI.Button("Button"); /* Put button specs in */
}
if(activeBlock){ /* Block is live */
GUI.Box("Box"); /* Put box specs in */
}
}
}


Also I realized the need to use the !. Makes sense if you are only using the mouseDown. Alternatively you could add an opposite mouseUp to swap things back, but both should do relatively the same thing. Although if you use the mouseUp you may end up with a very fast switch every time you click.