Disable buttons for individual game objects.

Hello,
I ran into a problem that I can’t seem to fix.
I have two scripts: One that displays a group of 3 buttons when I click a floor tile and another that displays 3 buttons when I click an object built on that tile.

The problem is that at the moment if I click multiple tiles/buildings in a row the buttons from the previous selected object stay active. Is there any way (apart from using object Tags) to prevent this from happening?

Here is the class that handles the button creation. As you can see from the code I temporarily fixed the issue by creating an access property for the boolean _isPressed and each time I try to display buttons in another location I grab an array of all other objects of that type and disable them.

I was wondering if there is any other way in which I can do this. Something perhaps more optimal or usable without tags? I fail to see a solution to this issue but perhaps now that you see the code you can help me a bit more.

public class BuildingMenu : Stats
{
private Vector3 _mousePos;
private bool _buttonsShown = false;
private GameObject _buildings;
private bool _isPressed = false;

private float X_POS;
private float Y_POS;
private float WIDTH;
private float HEIGHT;

void Awake()
{
	X_POS = 0;
	Y_POS = Screen.height - Screen.height / 3;
	WIDTH = Screen.width;
	HEIGHT = Screen.height / 3;
}	

void OnMouseUpAsButton()
{
	//building was clicked
	IsPressed = true;
	
	Debug.Log(GetComponent<Stats>().Health);
	
	//check if other buildings have buttons displayed and turn them off
	FlipButton();

	//freeze mouse position if buttons were already shown for this tile. This avoids a case where buttons move around each time they are clicked
	if(IsPressed && !_buttonsShown)
	{
		_mousePos = Input.mousePosition;
		_buttonsShown = true;
	}
}

public void FlipButton()
{
	_buildings = GameObject.FindGameObjectsWithTag("Wall");
	
	foreach(GameObject building in _buildings)
	{
		if(building.GetComponent<BuildingMenu>().IsPressed && IsPressed)
		{
			building.GetComponent<BuildingMenu>().IsPressed = false;
			IsPressed = true;
		}
	}
}

void OnMouseExit()
{
	_buttonsShown = false;
}

void OnGUI()
{
	drawButtons();
}

private void drawButtons()
{
	if(IsPressed)
	{
		if(GUI.Button(new Rect(Screen.width - (Screen.width - _mousePos.x) - 3 * NameConsts.BUTTON_OFFSET, Screen.height - _mousePos.y, NameConsts.BUTTON_WIDTH, NameConsts.BUTTON_HEIGHT), "Sell

" + Cost/2 + “gold”))
{
Destroy(gameObject);
IsPressed = false;
}

		if(GUI.Button(new Rect(Screen.width - (Screen.width - _mousePos.x) - NameConsts.BUTTON_OFFSET, Screen.height - _mousePos.y - 3 * NameConsts.BUTTON_OFFSET, NameConsts.BUTTON_WIDTH, NameConsts.BUTTON_HEIGHT), "Upgrade"))
		{
			IsPressed = false;
		}
		
		if(GUI.Button(new Rect(Screen.width - (Screen.width - _mousePos.x) + NameConsts.BUTTON_OFFSET, Screen.height - _mousePos.y, NameConsts.BUTTON_WIDTH, NameConsts.BUTTON_HEIGHT), "Destroy"))
		{
			IsPressed = false;
		}	
	}
}

public bool IsPressed
{
	get
	{
		return _isPressed;
	}
	set
	{
		_isPressed = value;
	}
}
},

Thank you for taking the time to read this.

You need to have something in your code that’s spawning the buttons that checks to see if you’ve already displayed them somewhere else and then removes those before showing the new ones.