- Home /
How to return UI element at mouse with RayCast
Hi everyone, So what I want to do is return the UI (Unity 4.6) element that the mouse is over when ever the following function gets called:
bool isUIObjectAtMouse(out GameObject objectAtMouse)
{
int layerMask = 1 << 5;
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, Mathf.Infinity, layerMask))
{
Debug.Log(hit.transform.gameObject.name);
objectAtMouse = hit.transform.gameObject;
return true;
}
else
{
Debug.Log("Nothing");
objectAtMouse = null;
return false;
}
}
But the problem is it never returns true. The elements do have 2DBoxColliders on them... I'm quite a novice with regards to RayCasts so any obvious mistakes that I am doing would be really appreciated :)
Comment
Your answer
Follow this Question
Related Questions
Unity 4.6.2 Editor's UI too small on high DPI screen 1 Answer
How to detect the position of a texture that is clicked 1 Answer
How to get UI element into HMD Unity 5.3.4p1 - Oculus Utilities 1.3 0 Answers
Create UI raycast like a mouse Click 2 Answers
Unity 4.6 ui scroll rect won't show up in fullscreen 1 Answer