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…
… and this is where it should be detected…
Why is this happening???