OnMouseOver() isn't working

I have a box collider on my blocks where the script is attached…

and the code in the script is.

using UnityEngine;

public class MouseDetect : MonoBehaviour
{
    void OnMouseOver()
    {
        Debug.Log("mouse is over" + gameObject);
    }
}

I don’t understand… seems pretty simple

I tried

using UnityEngine;

public class MouseDetect : MonoBehaviour
{
    void OnMouseOver()
    {
        Debug.Log("mouse is over" + gameObject.name);
        print("hello");
    }
}

but that didn’t do anything either

Under the hood, OnMouseOver() is using a raycast to shoot from the camera out into the world and see if it hits any collider. I usually do this manually because I forget about OnMouseOver, OnMouseEnter, and OnMouseExit.

However this only works if the collider is on the same object as your script (which it is) and nothing is blocking the raycast. I believe another collider may be getting hit instead of your desired object and it’s quite possible that this other collider may be a child under this object.

I would check your children and make sure there are no colliders on the child objects before proceeding.

Hi Anthony, thanks for looking into it like you did. I disabled all the Mesh Colliders in the quads that make up that cube, leaving that box collider as the only active collider, however, that didn’t work.

Hi Heath,

Is there an Event System in the Hierarchy of your scene?

no

Add one and test your game again.

it’s greyed out… how do I add it?

Could you share a screenshot, please?

Actually, you should be able to add it by clicking the right mouse button in the Hierarchy Window. It is either at the top level of the context menu, or in the UI submenu.

I added the event system but that didn’t work

can you use OnMouseOver() if you are using Cinemachine?

That’s a good question. Could you share a screenshot of your scene, please?

Is there a collider in front of the object you want to be able to detect with your mouse?

I would create a new scene for testing purposes with one object in it only, e.g. the cube voxel and the MouseDetect component. Test the “game” with this scene. Once that works, add a cinemachine and test your game again. If the code stops working, the issue is very likely the cinemachine.


Updated Tue Apr 21 2020 14:13

You could also log into your console what your mouse does detect. Use the C# example from this answer: http://answers.unity.com/answers/547532/view.html

it works with Cinemachine and virtual cameras…

I just tired to put that script on my enemies… and it doesn’t work on them either…

but that does work in the test scene…

I double checked there was nothing between my camera and the colliders I’m trying to detect… nothing in the way

Just to make sure I understood everything correctly: According to your test, the enemy gets detected without the cinemachine but it does not with the active cinemachine?

Which game object gets followed by the cinemachine?

Ok… found it… I just said there was nothing blocking it, however, the cinemachine confiner is a giant collider around the entire scene, which has some fudge to it with the damping setting… when I put both a Raycast script AND the OnMouseOver() script in the scene, the raycast worked… returning only the cinemachine confiner… if I disable it, the raycast and OnMouseOver() work on everything, remove the raycast, and I’m back to where I should be in the course, with my OnMouseOver() reporting a hit on everything with that script…

@Nina the thing is though… I set that confiner as a trigger… and I remember in the docs that OnMouseOver() won’t hit triggers… but I never looked at this…

Screen Shot 2020-04-21 at 7.45.00 AM

if I uncheck that… I can keep my confiner and everything works…

Thanks for you attention and help @Nina

Fantastic. Thanks for sharing your solution. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms