how to hit a gui button with raycast

i have an item that when dragged, but then clicked it sends ray
and if it hit a
collider of a specific tag it does this or that,
i want to add gui, if it hit.transform,
of gui, to do blahhh not important,
but how can i know i hit gui?
i have searched alot how to do this but come up alot
in searches, of guitext etc unrelated things, but still
i apologize if this is simple or been asked/answered somewhere already

what you want is GUIElement.HitTest

:slight_smile:

(not relevant with a GUI.Button though, for an actual button you just declare it in the if):

if(otherCriteria){
    if(GUI.Button(Rect...etc{
   //special button code
   } 
}

(similar to @robertbu 's answer, the area is simply defined by the Rect)

To the best of my knowledge, there no way to cast a ray on a GUI Button…they don’t have colliders, they live in the GUI coordinate system (not world coordinates), and they have no Z axis (i.e. you cannot get a perspective view on one). You can test if the mouse is over a particular button:

private var myRect = Rect(100,100, 350, 350);
function OnGUI () {
	 var e : Event = Event.current;
	 
	 if (myRect.Contains(e.mousePosition))
	 	Debug.Log("Moving over the button");

	if (GUI.Button(myRect, "My button"))
		Debug.Log("Button Pressed");
}