- Home /
Prevent raycast to OnMouseOver() or OnMouseEnter() when mouse over UI
As title says, it there anyway how to prevent Unity firing OnMouseOver() or OnMouseEnter() functions when mouse is over 4.6 UI objects?
Note, this cannot be stopped with UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject() since Unity handles calls to those functions.
$$anonymous$$ost, if not all UI elements have a little checkbox that says something like "Receive Raycasts". Try to uncheck that bad boy so it doesn't accept them
Answer by Fydar · Mar 04, 2016 at 08:45 PM
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
(. . .)
}
}
}
}
Your answer
Follow this Question
Related Questions
Make screen center behave as mouse pointer when cursor is locked? 0 Answers
make an object glow when mouse over 3 Answers
Block camera raycast by UI 1 Answer
Unity UI Mouse + Keyboard navigate, Un-Highlight button choice on mouse over 0 Answers
Want to move object slowly to where the mouse clicks? 1 Answer