catch GUI click to avoid raycasting after a click? pls help

Hey guys ! i’m new to this, so here is your chance ! Do a good deed and help a noob to get the “noob+1” achievement;)

i’m constantly run into the same problems with my projects -.-’ … i want to select an object in my scene by clicking it. After that there should appear a GUI menu where i could do any kind of stuff. Clicking and selecting a building is no problem i found lots of help for that. But when i try to click a button at the menu my code recognizes a click, casts a ray, deselect my object and ignoes the GUI.Button click -.-’ now i need a solution for taht … :-/ at my last project i listened for any click above the menu rect but now i have a dynamic and dragable menu and this solutions seems a bit sloppy to me :wink: Now i tried to set a bool after the button click and check for that in SelectionManager() but it seems that the window disappears before the button click happens -.-’
A friend of mine mentioned the event.use stuff … but i don’t get that at all and it isn’t working (because i have no idea how to use that) o.0

here is my code of my menu script:

using UnityEngine;
using System.Collections;

public class BuyBuilding : MonoBehaviour {
	
	private Building building;
	
	private const int BUY_WINDOW_ID = 0;
	
	private const string BUY_BUTTON_LABEL = "Buy Building";
	private const string BUY_WINDOW_LABEL = "Buy ";
	
	private Rect _buyWindowRect = new Rect( 10,100, 150,250);
	
	
	private void Start()
	{
		building = gameObject.GetComponent<Building>();	

	}
	
	private void OnGUI()
	{
		if(building._isSelected)
		{
			_buyWindowRect = GUI.Window( BUY_WINDOW_ID, _buyWindowRect, ShowBuyWindow, BUY_WINDOW_LABEL + building.name);
		}
	}
	
	
	private void ShowBuyWindow(int windowID)
	{		
		if(GUI.Button(new Rect(0, _buyWindowRect.height-25, _buyWindowRect.width, 25), BUY_BUTTON_LABEL))
		{
			
			building.gm.anyButtonPressed = true;
			building.SetBuildingStatus(Building.BuildingStatus.PlayerBuilding);
			PlayerClass.curMoney -= building.curBuildingValue;
			Destroy(this);
			
		}
		
		GUI.DragWindow();	
	}

}

and the Manager script :

public GameObject selectedObject;
public bool anyButtonPressed = false; 

void Update () {
	SelectionManager();
	}
	
	private void SelectionManager() {	
		
		if(Input.GetMouseButtonDown(0))
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			
			Debug.Log("checking for hit");	

		RaycastHit hit;
		if(Physics.Raycast(ray, out hit, 100))
		{
				if(anyButtonPressed)
				{
					Debug.Log("returning");
					return;
				}
				else
				{
					if(hit.transform.tag == "Building")
					{
						if(selectedObject!= null)
						{
							if(selectedObject != hit.transform.gameObject)
							{
								selectedObject.SendMessage("SelectBuilding", false);
								selectedObject = hit.transform.gameObject;
								selectedObject.SendMessage("SelectBuilding", true);
							}
							else
							{
								return;
							}
						}
						Debug.Log("Hit Building");
						selectedObject = hit.transform.gameObject;
						hit.transform.SendMessage("SelectBuilding", true);
					}
					else
					{
						if(selectedObject!= null)
						{
							selectedObject.SendMessage("SelectBuilding", false);
							Debug.Log ("Deselect " + selectedObject.name);
						}
						selectedObject = null;	
					}
				Debug.Log("hit");		
				}
			}
			
		}
	}

a … an answer in c# would be AWESOME :smiley: hope you hav a heart for a helpless little newbie :slight_smile:

Best and thanks in advance!!
Cxyda

There are a number of ways you can approach this problem, and which one depends on how you want your menus to function.

The easiest would be to disable selecting in the manager script if something is already selected. Then in the BuyBuilding class, the object would be deselected when the menu is taken down.

Another way would be to communicate between the selecting code and the Manager script. You already do so between the Building script and the BuyBuilding script. The mechanism is just the same.

Another way would be to use C# events and delegates. Anytime a menu comes up or goes down, a event would fire, and any code that was interested could take note.

And finally here is a hackish way. Place a box collider in front of the camera and make it a child of the camera. Any time a menu comes up, the box collider would be enabled. The selection code would bail out if it hit the box collider.