Unity Gui has me in a choke hold...

Well here I was making a decent attempt at a tower defense game then along came unity GUI and made me its bitch so I could use a hand. I’m trying to make like an upgrade system for my towers. I have buttons that when push allow you to place a tower or other structure, but I want a button or group of buttons to be around a selected structure to give options for upgrading or selling. Placing GUI objects near a selected structure and then linking those buttons to the said structure seems to be having me run in circles.

Is it possible to use GUI buttons like this or should I just make physical game Object buttons and have them deactivated and as children of my structures, then activate on structure selection? All I know is trying to make the GUI similar to what I want so far has been harder then making the game playable.

Any ideas are greatly appreciated. Thanks.

I would keep a boolean var “selected” that you toggled depending on whether or not the building is currently selected, and use WorldToScreenPoint to get the screen position (in pixels) of the currently selected structure when it is selected.

Then in OnGUI() you would check if the building is selected, and if so, position your buttons relative to the object’s 2d position (you’ll have to do some simple math to position the buttons around the object).

Here’s an example (for the sake of example, I’m toggling the selected var when the object is being hovered over with the mouse):

private var selected : boolean = false;
private var screenPos : Vector3;

function OnMouseEnter(){
	selected = true;
	screenPos = Camera.main.WorldToScreenPoint(transform.position);
}

function OnMouseExit(){
	selected = false;
}

function OnGUI(){
	if(selected){
		GUI.Button(Rect((screenPos.x - 100), screenPos.y, 50, 50), "Button"); // button 100 pixels to the left of object
		GUI.Button(Rect(screenPos.x, (screenPos.y + 100), 50, 50), "Button"); // button 100 pixels above object
		GUI.Button(Rect((screenPos.x + 100), screenPos.y, 50, 50), "Button"); // button 100 pixels to the right of object
		GUI.Button(Rect(screenPos.x, (screenPos.y - 100), 50, 50), "Button"); // button 100 pixels below object	
	}
}