Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by skyx26 · Feb 16, 2015 at 09:54 AM · c#unity 4.6onmouseenter

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...

... and this is where it should be detected...

Why is this happening???

detection.png (7.2 kB)
box.png (2.5 kB)
Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Feb 16, 2015 at 12:14 AM 0
Share

"GUIElements" are the Legacy GUI system.

avatar image skyx26 · Feb 16, 2015 at 12:31 AM 0
Share

@meat5000 I'm not using GUIElements. I'm using the new UI text component...

avatar image meat5000 ♦ · Feb 16, 2015 at 02:07 AM 0
Share

Read the quote you posted, then read my comment.

avatar image skyx26 · Feb 16, 2015 at 02:27 AM 0
Share

@meat5000 I quote again ""On$$anonymous$$ouseEnter is called when the mouse entered the GUIElement OR Collider." I'm NOT using a GUIElement as I told you. If you read http://docs.unity3d.com/ScriptReference/UI.Text.html you will see that On$$anonymous$$ouseEnter() is available so it can be used on a Text component of the new UI. Now, if you don't use a GUIElement you must use a collider, wich is my case.

avatar image meat5000 ♦ · Feb 16, 2015 at 02:48 AM 0
Share

Ok thanks for the clearup.

Have you tried this in build or only in editor?

Excuse the question but did you click in the Game window before you tried hovering over the relevant object?

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Mmmpies · Feb 16, 2015 at 10:09 AM

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Feb 16, 2015 at 08:06 PM 0
Share

Or you could use the IPointerEnterHandler interfaces directly.

avatar image skyx26 · Feb 16, 2015 at 09:27 PM 0
Share

Sorry man, I didn't saw your answer until now. $$anonymous$$y mailbox set lastest mail first, so yours was the last. $$anonymous$$y thanks goes to you too for help me with that problem. No hard feelings?

avatar image Mmmpies · Feb 16, 2015 at 09:37 PM 1
Share

No hard feelings at all :¬)

Answers can, occasionally, be a bit flaky. Questions getting stuck in moderation with no way of rejecting or approving them and such like so I was just wondering if there was something wrong with my account.

Regularly wake up to over 100 e-mails so know that feeling of not spotting one as well.

Glad it helped and glad you thanked @Bored$$anonymous$$ormon and @meat5000 as well, both good people.

avatar image
2

Answer by Kiwasi · Feb 16, 2015 at 08:13 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image skyx26 · Feb 16, 2015 at 08:54 PM 0
Share

OH $$anonymous$$Y $$anonymous$$AN I LOVE YOU!!!

Your answer point me in the right direction. Ins$$anonymous$$d of using On$$anonymous$$ouseEnter() I selected the text component and added a new event trigger component, point enter type, and the function I want to call. No need for ray casting, no need for colliders, and work flawlessly no matter where the camera is or the resollution used. Thank you so much!

avatar image
0

Answer by rmesquit · Jun 30, 2016 at 05:20 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Dispose Function in c# Unity 0 Answers

Canvas Button OnClick() function arguments 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges