OnMouseEnter triggering on wrong object
Hi all,
I'm having an issue where OnMouseEnter seems to be triggered on wrong object. I have a MonoBehaviour class that contains OnMouseEnter method that changes color of object it is attached to.
public class InventoryCell : MonoBehaviour
{
public void OnMouseEnter()
{
Debug.Log($"Enter {gameObject.name}");
_material.color = _configuration.HoverColor;
}
}
This class is attached to all objects representing inventory slots. However, when I hover mouse incorrect object changes color. It's like there's some weird offset being added to mouse position when calling the event. The further from center of the screen the mouse is the farther object OnMouseEnter is called
Doubled checked and I have all colliders setup correctly, none of them are interfering with one another (in video you may notice that collider scale is 1.05 - it is made so to fill gaps between slots, changing collider scale to 1 does not solve the issue). Worth mentioning, objects being hit are rendered by overlay camera not base one.
I only wrote about OnMouseEnter, however, the same behaviour is happening with other OnMouseXXX methods as well.
Uploaded video recording of the issue here: https://1drv.ms/v/s!Ap4-t2P-IgziiNUlWkyNNYEjp8QbIQ?e=tSAkG6
Any idea what may be the cause of this?
Answer by GintarasOE · Dec 30, 2021 at 11:04 AM
Hi all,
Figured out what the problem was so leaving answer here, maybe it will help someone in the future.
The issue was caused by Camera previously being set to Render Type: Base and its Viewport Rect edited. It seems like a Unity bug to be honest (will report it). When you switch to Render Type: Overlay, Viewport Rect options are no longer available for editing (they are no longer shown in Inspector), yet, they still affect the camera behavior.
Had similar issue previously where some objects were being clipped out because they were going out of Viewport Rect in Overlay camera. Unity should ether not hide Viewport Rect options when switching to Overlay camera mode or make so it no longer has any effect... wasted whole day on this...
Your answer
Follow this Question
Related Questions
OnTriggerExit2D is being ignored! (I think I need to retarget the function but not sure how) 1 Answer
Im trying to turn off a sound when i leave a trigger collider 0 Answers
Is there a way of locking the cursor inside a radius of a circle. 0 Answers
Instantiating UI element at mouse position on click 1 Answer
Can't get raycast to mousePosition 0 Answers