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 GODSBANE · Jul 09, 2014 at 03:11 PM · mouseonmouseexit

Mouse never leaves Collider

I have a Textmesh with a boxcollider on it which is sized just fine. When my mouse enters the collider, the OnMouseEnter() and the OnMouseOver() methods begin to execute. Nothing wrong there. OnMouseDown() also works perfectly. The only problem that I have is when my mouse goes to leave the collider area. The script never realizes that the mouse actually leaves. I tested this with a little code like so:

     public bool isSelected = false;
     TextMesh myText;
 
     void Start()
     {
         myText = gameObject.GetComponent<TextMesh>();
     }
 
     void OnMouseEnter()
     {
         isSelected = true;
     }
 
     void OnMouseDown()
     {
         Application.LoadLevelAdditive("");
         print("Click");
     }
 
     void Update()
     {
         if (isSelected)
             myText.color = Color.yellow;
         else
             myText.color = Color.white;
 
         isSelected = false;
     }

When the mouse enters the collider, isSelected becomes true and the color of the text changes to Yellow, so I know that part works, but with it on OnMouseEnter(), I can check the status of the mouse entering every frame (I know that it should be OnMouseOver() later, this is for debug purposes.) Now,on the second frame after the mouse enters the collider, it changes to the color to White because the isSelected variable is set to false now. Great, works fine.

When the mouse supposedly leaves the collider, and reenters, the OnMouseEnter() method doesn't fire because it thinks the mouse is still in the collider for some weird reason. Any suggestions as to why this might be, and potential fixes?

P.S. I can get the OnMouseExit() function to work properly on another script in a different scene.

Comment
Add comment
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

2 Replies

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

Answer by GODSBANE · Jul 09, 2014 at 04:36 PM

Interesting observation and answer to my question.

OnMouseExit() does not occur until the mouse enters a new collider different from the one which it is currently in.

So, to solve this problem, you have to have a collider besides the one with the OnMouseEnter()/OnMouseOver(), and mouse over that collider for the OnMouseExit() method to actually occur.

Not sure if this is how Unity intended for it to work, but this is the solution for now at least.

Comment
Add comment · Show 2 · 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 robertbu · Jul 09, 2014 at 04:54 PM 0
Share

Not true. I tested the code I posted below in an empty scene with just a Text$$anonymous$$esh and it worked fine. And it has worked fine for any version of Unity that I've used On$$anonymous$$ouseExit(). If you have a single scene with a single object that is exhibiting this behavior, then you need to submit a but report to Unity. To verify:

  • Start a new scene

  • Create a 3D Text/Text$$anonymous$$esh

  • Add a BoxCollider component

  • Add my script below

  • Run the app

If this simple setup has your issue, submit a but report.

Note you would have the behavior you describe if your collider on the Text$$anonymous$$esh was oversized and the new colliders were in front of the Text$$anonymous$$esh.

avatar image GODSBANE · Jul 16, 2014 at 04:08 PM 0
Share

Okay, try setting the clear flag on your camera to Depth only and tell me if your solution works.

avatar image
0

Answer by robertbu · Jul 09, 2014 at 03:30 PM

The way you have things structured right now, the first frame after you set 'isSelected' to 'true', the Update() function turns the text yellow. But since you set 'isSelected' to 'false' in the Update() loop, the text goes back to white the very next frame. Why not so something like this instead:

 TextMesh myText;
 
 void Start()
 {
     myText = gameObject.GetComponent<TextMesh>();
 }
 
 void OnMouseEnter()
 {
     myText.color = Color.yellow;
 }


 void OnMouseExit()
 {
     myText.color = Color.white;
 }

 void OnMouseDown()
 {
     Application.LoadLevelAdditive("");
     print("Click");
 }
Comment
Add comment · Show 2 · 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 GODSBANE · Jul 09, 2014 at 03:40 PM 0
Share

Because as I said before, this is for debug and the On$$anonymous$$ouseExit() event never occurs.

avatar image robertbu · Jul 09, 2014 at 04:49 PM 0
Share

Well, I tested the above code before I posted it, and it works fine. If you are having trouble with On$$anonymous$$ouseExit(), please post the script that has the issue. The most common issue with On$$anonymous$$ouse* functions not firing is that the function name is spelled wrong. If you want to make it work without using On$$anonymous$$ouseExit(), you need to use On$$anonymous$$ouseOver() and then figure out how to set the value to white before the possible On$$anonymous$$ouseOver() call. Something like:

 Text$$anonymous$$esh myText;
 
 void Start()
 {
     myText = gameObject.GetComponent<Text$$anonymous$$esh>();
     StartCoroutine(BackToWhite());
 }
 
 void On$$anonymous$$ouseOver()
 {
     myText.color = Color.yellow;
 }

 IEnumerator BackToWhite()
 {
     while(true) {
         myText.color = Color.white;
         yield return new WaitForEndOfFrame();
     }
 }

 void On$$anonymous$$ouseDown()
 {
     Application.LoadLevelAdditive("");
     print("Click");
 }

Note that 'WaitForEndOfFrame()' returns control after all the rendering of the current frame is completed.

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

21 People are following this question.

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

Related Questions

OnMouse menu 1 Answer

OnMouseExit Problems vs. RaycastAll 2 Answers

How do I play a sound after the mouse goes through an object? 3 Answers

Mouse Orbit with up/down based on target's up/down 0 Answers

Input Mouse? 2 Answers


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