0
\$\begingroup\$

I'm working on a UI-heavy game and I'd like to add sound effects for when the player mouses over a UI button (using TMP, if that matters). I've tried OnMouseOver and its siblings OnMouseEnter and OnMouseStay, I've tried with and without 2D Box Colliders, but so far nothing I've tried seems to register that the mouse is over the button: i.e. nothing happens. If a collision or Raycast hit were registered, all that I'm asking right now is for it to run the line Debug.Log("Collision detected");

I know that Unity is able to register hits on the UI element I'm testing on, because mousing over it engages the Sprite Swap function that's part of the Button component, which it would not do if it were not accepting Raycast hits.

I'm sure there's some very simple solution to this, like a check-box I haven't ticked, but I could really use some help here.

Many thanks!

\$\endgroup\$

3 Answers 3

1
\$\begingroup\$

Maybe try void OnPointerEnter(PointerEventData eventData){}? It comes with

using UnityEngine.EventSystems;
using UnityEngine.UI;
\$\endgroup\$
1
  • \$\begingroup\$ Mark as answer. And I'm glad to help ^_^ \$\endgroup\$
    – Dead Lord
    Commented Dec 26, 2020 at 9:05
1
\$\begingroup\$

That did it. Thanks Dead Lord! Here's the code that worked.

using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine;

public class Test : MonoBehaviour, IPointerEnterHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
    Debug.Log("Mouse detected");
    }
}
\$\endgroup\$
0
\$\begingroup\$

This may also happen if you forget to disable the 'raycast target' checkbox on images & text that can block the mouse over raycast.

Raycast Target

Since you are using TMP: the option is found in the inspector and located via the extra settings dropdown

enter image description here

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .