Reputation: 289
This script gives a message on the console only if the hit gameobject is not in a canvas. When the mouse button is released on a button located inside a canvas, the script doesn't debug anything. How can I fix this?
RaycastHit hit;
void Update ()
{
if(Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//RayHit hit;
if(Physics.Raycast(ray, out hit))
{
// do what you want
Debug.Log(hit.collider.gameObject.tag);
}
}
}
Upvotes: 1
Views: 705
Reputation: 1099
You can use like this to get which UI object is clicked.
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log(EventSystem.current.currentSelectedGameObject.GetComponent<Text>().name);
}
EventSystem.current.currentSelectedGameObject.GetComponent().name
will return clicked object and
EventSystem.current.IsPointerOverGameObject()
will check if an UI object is click or not.
Upvotes: 1