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.
  2. Dismiss Notice

Question OnMouseOver() not working on buttons

Discussion in 'Editor & General Support' started by NormyHaddad, Jun 25, 2023.

  1. NormyHaddad

    NormyHaddad

    Joined:
    Jan 10, 2022
    Posts:
    13
    I want to create a tooltip that appears when the mouse hovers over a button. I tested it and it follows the mouse just as I want it to but the problem is that when I try putting my cursor over a button, nothing happens. I tried attaching rigidbodies and colliders to it (both 2d and 3d), but nothing happens, even though the button changes colour when the mouse goes over it.
    Here's the code i'm using. The script is attached to the button object, but there's nothing in the console from debug.
    Code (CSharp):
    1.  private void OnMouseOver()
    2.     {
    3.         Debug.Log(1);
    4.     }
     
  2. Nstdspace

    Nstdspace

    Joined:
    Feb 6, 2020
    Posts:
    26
    For Ui Elements you want to use
    IPointer*Handler
    instead.
    For example:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.EventSystems;
    3.  
    4. public class MouseMoveBehaviour : MonoBehaviour, IPointerMoveHandler {
    5.  
    6.     public void OnPointerMove(PointerEventData eventData)
    7.     {
    8.          Debug.Log("mouse moving");
    9.     }
    10. }
    For an overview of all available interfaces refer to this list.
     
    spiney199 likes this.
  3. NormyHaddad

    NormyHaddad

    Joined:
    Jan 10, 2022
    Posts:
    13
    is there any way i can make the handler detect only certain objects?
     
  4. Nstdspace

    Nstdspace

    Joined:
    Feb 6, 2020
    Posts:
    26
    Can you explain more about your specific scenario?
    Do you mean detecting only specific objects which the mouse interacted with?

    The callback function
    OnPointer*
    is invoked when the respective interaction happens on the object the script is attached to or a child of it.
    If you want to specific children you have to disable the "raycast target" property of the Image components:

    upload_2023-6-28_18-5-2.png

    This will disable all the interactions, so you might not want this.
    For more fine-grained control you would could implement the interface multiple times and implement some custom logic - but it really depends on what exactly you want to do.
     
  5. NormyHaddad

    NormyHaddad

    Joined:
    Jan 10, 2022
    Posts:
    13
    No, I have a thing where a tooltip appears under the cursor when the mouse is over a button. This tooltip is a panel and gives more info about the button so i can use icons on the button instead of text. But if i put the tooltip directly under the cursor, then the button thinks the cursor is no longer over it because it's over the panel instead, so the panel deactivates, which causes the cursor to be over the button which activates the panel and so on.