Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

MouseOver event

Discussion in 'Getting Started' started by Poinsetia, Dec 21, 2017.

  1. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    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
    Code (CSharp):
    1. using UnityEngine;
    2. public class OnMouseOverExample : MonoBehaviour
    3. {
    4. void OnMouseOver()
    5. {
    6. //If your mouse hovers over the GameObject with the script attached,
    7. output this message
    8. Debug.Log("Mouse is over GameObject.");
    9. }
    10. void OnMouseExit()
    11. {
    12. //The mouse is no longer hovering over the GameObject so output this
    13. message each frame
    14. Debug.Log("Mouse is no longer on GameObject.");
    15. }
    16. }
    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

    Regards
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,507
    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:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class OnMouseOverExample : MonoBehaviour {
    4.     void OnMouseOver() {
    5.         //If your mouse hovers over the GameObject with the script attached, output this message
    6.         Debug.Log("Mouse is over GameObject.");
    7.     }
    8.     void OnMouseExit() {
    9.         //The mouse is no longer hovering over the GameObject so output this message each frame
    10.         Debug.Log("Mouse is no longer on GameObject.");
    11.     }
    12. }
     
  3. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    Yes it looks like that in the editor. It throws no error. It just doesn't work :)
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,507
    Is your element a GUI element or an object with a collider attached to it?
     
  5. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    It is an object with a collider attached to it :)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)
     
  7. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    I must be missing something. Could you tell me step by step what you are doing? I would really apreciate it. Thanks in advance. :)

    Regards
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    All I did was make a cube in the scene, copied your script, attached it to said cube, and voila! :)
     
  9. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    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? :)
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
  11. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    In the physicsManager 'queries hit triggers' is marked. And in the box collider' is trigger' is also marked. :)
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  13. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    No. :) I am testing it just pressing the play button. It has to be missing something stupid but very important at the same time.
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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.
     
  15. Poinsetia

    Poinsetia

    Joined:
    Dec 9, 2016
    Posts:
    18
    I clicked outside the cube and then I move the mouse over the cube and even pressed it but it doesn't respond. :)
     
  16. TheMatrixAgent22

    TheMatrixAgent22

    Joined:
    Jul 9, 2017
    Posts:
    25
    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.
     
  17. kath-bruce

    kath-bruce

    Joined:
    Mar 31, 2020
    Posts:
    1
    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.

    Code (CSharp):
    1. public class MyScript : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
    2. {
    3.         public void OnPointerEnter(PointerEventData data)
    4.         {
    5.             Debug.Log("enter");
    6.         }
    7.  
    8.         public void OnPointerExit(PointerEventData data)
    9.         {
    10.             Debug.Log("exit");
    11.         }
    12. }
    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.

    Hope this helps!
     
    Last edited: Jul 5, 2020
  18. jpveix

    jpveix

    Joined:
    Aug 28, 2020
    Posts:
    1
    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.

    How this helps the next person!
     
    Benjitech likes this.
  19. Digimaster

    Digimaster

    Joined:
    Aug 8, 2017
    Posts:
    6
    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.

    I am using Unity-Version 2020.3.541.

    I hope this is a useful information to somebody!
     
  20. Benjitech

    Benjitech

    Joined:
    Feb 18, 2022
    Posts:
    1
    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.