Hello everybody. I know I have asked this and I have tried the suggested andswers but still it doesn’t work. I don’t know how to apply a MouseOverEvent to an object. I have tried attaching the MouseOver code to the collider. The object is marked as “trigger” and the Physics.queriesHitTriggers is true. This is the code
using UnityEngine;
public class OnMouseOverExample : MonoBehaviour
{
void OnMouseOver()
{
//If your mouse hovers over the GameObject with the script attached,
output this message
Debug.Log("Mouse is over GameObject.");
}
void OnMouseExit()
{
//The mouse is no longer hovering over the GameObject so output this
message each frame
Debug.Log("Mouse is no longer on GameObject.");
}
}
I have tried also with UI buttons, but the problem is that you cannot apply MouseOver to a button
I have tried using a complement “event trigger” pointer, and selecting the same code. But again it doesn’t work. I need the explanation step by step. Any clue? Thanks in advance
Is this the actual code attached to your script? Because if so, this will throw errors because of lines 7 and 13, which are supposed to be part of the comment. It should look like this:
using UnityEngine;
public class OnMouseOverExample : MonoBehaviour {
void OnMouseOver() {
//If your mouse hovers over the GameObject with the script attached, output this message
Debug.Log("Mouse is over GameObject.");
}
void OnMouseExit() {
//The mouse is no longer hovering over the GameObject so output this message each frame
Debug.Log("Mouse is no longer on GameObject.");
}
}
Not sure what is happening for you, but the code is working for me.
I was curious if it required a physics raycaster, like the interfaces, but it doesn’t
It doesn’t work. There must be something missing. How do you attach the script? I dragged the script to the cube. Did you give any special name to the script? It is nonsense. I just tried once and again. I have the personal edition. Any idea?
I just dragged the script, like you. There is no special name required.
Are you sure you have the physics setting : Queries hit trigger enabled? Mine of course failed, with that off.
Beyond that, I’m not sure what to say, sorry.
Personal edition/plus/pro make no difference on the code.
Well, I’m at a loss. Everything sounds good to me.
Just out of curiousity, if you change the cube’s collider to not a trigger, does the script work then?
hm. very strange. Made sure to click in the game view window before putting the mouse over the object?
the game view window will lose focus if you click off of it , and not intercept events until you “click” it back into focus. Now, I’m really reaching at what is wrong lol.
I know this thread is old, the problem could be that the layer was set to “Ignore Raycast”. That would prevent it from working. Anyone looking for an answer, check that.
So I had the EXACT same problem - I managed to get it working by adding a Physics 2D Raycaster to the camera and use IPointerEnterHandler and IPointerExitHandler interfaces on the game object.
public class MyScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
public void OnPointerEnter(PointerEventData data)
{
Debug.Log("enter");
}
public void OnPointerExit(PointerEventData data)
{
Debug.Log("exit");
}
}
Never could figure out why the OnMouseEnter and OnMouseExit methods didn’t work.
Make sure that you have a collider, either 2d or 3d, on your gameobject and that the “queries hit triggers” option is ticked (there are separate options for 2d and 3d physics - that might catch you out). The “is trigger” option on the collider doesn’t strictly have to be ticked, however - at least just testing it now on my project, that appears to be the case. The physics raycaster on the camera has to match the collider on the gameobject, as well as the physics “queries hit trigger” option, so 2d raycaster on the camera, a 2d collider and “queries hit trigger” ticked on Edit > Project Settings > Physics 2D.
Ok, here is what I have discovered while testing out this feature (OnMouseOver)…
If you have installed and ONLY activated the new Input System these functions WILL NOT WORK
I had installed the new Input System to prototype and learn it and then stumbled upon the section in the documentation about OnMouseOver. I was anxious to try them out but to my dismay they were not doing anything…
Since I had just installed the new Input System (and disabled the old) I wondered if that was related. Guess what… IT IS…
So if you install the Input System Package and want to activate it as well as the old Input Manager you need to set it in Projects Settings > Player > Configuration > Active Input Handling where you will have a dropdown allowing you to use either the old or the new system and also have both. I selected both. Now the mouse over triggers are working.
Also please double check that the camera z-position (so along the depth of the camera) is placed before (< not <=) the Gameobject you want to interact with the mouse event. E.g. when the Gameobject is at Position z = 0, change the camera z-position to z = -1 (in my case this is no issue as I use an orthographic projection).
It took me a while to figure this out, as in the Game-Window, the Gameobject is still visible through the Camera even when the z-Position of both is the same.
In my case, OnMouseOver() (and OnMouseExit()) are working with setting “Active Input Handling” to “Input Manager (Old)” as well as “Both”. I have to enable “queries hit trigger” and need a collider for my 2D-Sprite. However, I don’t have to use a “Physics2DRaycaster” Component for the Main Camera.
Oh my goodness! Thank you for your comment. I didn’t even consider how changing the input system would cause issue with this, as I had switched over to the new input system a while back in my project.