- Home /
My mouse on two overlapping GameObjects
Hello,
I would like to know how to get the full information when my mouse is on two overlapping Image GameObjects in an unique 2D Canvas, as "OnPointerEnter" won't work for both.
For example, with these blue and green round images, i would like a script that aknowledge when the mouse is in the red zone (aka "also in the blue image zone") :
Thanks !
Answer by CodeMonkeyYT · Apr 25 at 06:24 AM
You can use OnPointerEnter
and OnPointerExit
to know when it enters and exits each circle, just store a bool isMouseInside;
Then some other script would test
if (circleA.IsMouseInside()) && circleB.IsMouseInside()) {
// Do something
}
Alternatively you can manually fire the raycast yourself every frame and test if both are hit.
PointerEventData pe = new PointerEventData(EventSystem.current);
pe.position = Input.mousePosition;
List<RaycastResult> hits = new List<RaycastResult>();
EventSystem.current.RaycastAll( pe, hits );
Thanks, but OnPointerEnter
can't work on both overlapping GameObjects at the same time, as the green one behind will trigger OnPointerExit
when my mouse go in the red zone (where the blue one OnPointerEnter
will trigger).
I didn't test Raycast but wouldn't it be the same problem ?
Oh right forgot about that, in that case the Raycast method will work since it gives you all the raycast hits, not just the first one
Your answer
![](https://koobas.hobune.stream/wayback/20220613055804im_/https://answers.unity.com/themes/thub/images/avi.jpg)