OnMouseEnter NOT working

Hi guys, maybe I’m the most stupid man in the whole world, maybe I’m just doing something wrong, or maybe I don’t understand the logic behind OnMouseEnter.

I have an UI text component and I want to change it’s size when I touch it with the mouse. I search the API and found OnMouseEnter(). I quote “OnMouseEnter is called when the mouse entered the GUIElement or Collider.”, so I understand that whatever happens will happens ONLY when the mouse touch the GUIElement or the Collider.

With this in mind I add a new component: A box collider 2D. I set the size so it wrap the text and used the following code to test if everything was ok…

    void OnMouseEnter()
        {
            Debug.Log("Detected");
            print(gameObject.name);
        }

but it wasn’t, and I’m getting crazy because I don’t understand why is not working…

I read a couple of answers and all of them points in the same direction: you don’t have a collider, or, your scene is fucked up, create a new scene and try again.

I create a new scene and add a cube, attached the script to the cube and the OnMouseEnter() event work almost flawlessly. I found that if the mouse cursor passes over the cube so fast the OnMouseEnter() was never called, even if the mouse cursor passes over the cube fast first and then slowly, the OnMouseEnter() is never called… until I click the cube, then Unity3D remember that there was a box collider and an OnMouseEnter() event, and correctly redetect everytime the mouse cursor enter the box collider.

I delete the cube and add a UI button, attached the script and it didn’t work… or at least thats what I thought, after moving the mouse cursor randomly aroud the screen I notice the script attached sending the info to the console, so it was working, but not when the mouse cursor entered the box collider wrapped around the button, but when it was ABOVE the button.

I reload the original main menu escene and carefully move the mouse over the “MENU” word, and once again the console show something… ABOVE THE U LETTER.

Somehow the box collider is detecting the mouse cursor way up where it should be detected.

This is where the OnMouseEnter() is beign detected…

40813-detection.png

… and this is where it should be detected…

40814-box.png

Why is this happening???

For the new UI try highlighting the text and Add Component → Event trigger.

Now create a script with a public function that does what you want.

public void MrPointyIsHere ()
{
    // mouse enter code
}

public void MrPointyGone ()
{
    // mouse left code

Drag that script onto the text. In the event trigger Add New Event select Pointer Enter.

A slot appears so drag the button on the slot. Now from the drop down select YourScriptName → MrPointyIsHere

Do the same for Pointer Exits and MrPointyGone

Sorry for any typos, on my phone!

OnMouseEnter is not really meant to be used with the UI system. You’ll find the function will probably be deprecated in future versions.

The correct way to do this since 4.6 onwards is to implement the EventSystems.IPointerEnterHandler interface on your monobehaviour. Use the functions from this interface instead of OnMouseEnter. This also means you will need to have the appropriate GraphicsRaycaster for UI elements, and a PhysicsRacaster for regular colliders.

Go to the collider properties in the Inspector, make sure you can see the “Scene” view, adjust the collider’s x, y, z and radius. Make sure it’s in the same position as the GameObject you are trying to detect the collision, and make sure the “Is Trigger” option is ticked.