Around the code you are processing the raycast do an if statement to check a bool which monitors whether the mouse is over the UI element. If you know how to use event systems, you should be fine, but if your not, here is a start:
public class RaycastingScript : MonoBehaviour {
public bool hovering = false;
public void OnEnter () {
hovering = false;
}
public void OnExit () {
hovering = true;
}
void Update () {
if (hovering) {
if (physics.raycast(...)) {
//INSERT RAYCAST CODE HERE
(. . .)
}
}
}
}