- Home /
2D OnMouseOver issues
I'm currently trying to get two variations of OnMouseOver()
to work, however no OnMouseXXX()
function would work for me.
I was trying to use OnMouseDown()
a while back which wouldn't work for me at all, I eventually got this to work:
if (Input.GetMouseButtonDown(0))
{
Vector2 p = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
Collider2D[] hits = Physics2D.OverlapPointAll(p);
for (int i = 0; i < hits.Length; ++i)
{
//content
}
}
However now I'm coming back to trying to get OnMouseOver()
to work as I need to play a selection sound when the user hovers over mouse elements. This is all that I've tried:
void OnMouseOver()
{
selectSound_.Play();
}
I had previously tried more but I erased all of that code a few weeks back when I managed to get the Input.GetMouseButtonDown(0)
code working.
Does anyone have any ideas as to why OnMouseOver()
doesn't work for me? Or perhaps an alternative?
Note: When using OnMouseOver()
the function never fires, and I have both colliders and GUI elements.
Note that because of the way you have this structured, you will call 'Play()' every frame while the mouse is over the object. You tested your problem with a Debug.Log()? Any chance there is another (invisible) collider between the camera and the object with the On$$anonymous$$ouseOver() script? Your script would work with another collider since it only test if a collider is there not the collider. And the On$$anonymous$$ouseOver must be on the same game object as the collider, and that collider must be front-most with respect to the camera.
I did all of my testing with Debug.Log()
to no avail, and I do know play will be called every time, I plan to fix that so it'll only fire once.
I have checked for outside colliders that would interfere and there aren't any. When you say the On$$anonymous$$ouseOver() has to be on the same game object as the collider, what do you mean exactly? Do you mean I need to have an On$$anonymous$$ouseOver() attached to every object that I want to have something happen with? If this is the case, that could very well likely be my issue.
Edit: To add on to this however, I have tried all of the On$$anonymous$$ouseOver()
On$$anonymous$$ouseEnter()
, etc with GUI elements, which isn't working for me either.
For the use of On$$anonymous$$ouse??? functions, the script must be on the object with the collider, so you will need a script attached to every object that you want to detect the On$$anonymous$$ouse??? behaviors. And unlike your Raycast() code, only the front-most collider will fire.
What type of collider you have on your game object with the On$$anonymous$$ouseEnter() script attached? Is it 2d or 3d?