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\$
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
    Dec 26 '20 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.

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

Raycast Target

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.