I use this code to get an object clicked by the mouse
void Update() {
if (Input.GetMouseButtonUp(0)) {
RaycastHit hit;
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
Transform objectHit = hit.transform;
}
}
}
This code works even when I click the button and do not want to click an object that is behind the button. How to avoid it?