- Home /
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.
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.
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.
Okay, try setting the clear flag on your camera to Depth only and tell me if your solution works.
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");
}
Because as I said before, this is for debug and the On$$anonymous$$ouseExit() event never occurs.
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
Follow this Question
Related Questions
OnMouseExit Problems vs. RaycastAll 2 Answers
OnMouse menu 1 Answer