TransWikia.com

OnMouseOver not registering collisions

Game Development Asked by Hyperific on January 29, 2021

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!

3 Answers

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

Answered by Justin Markwell on January 29, 2021

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");
    }
}

Answered by Hyperific on January 29, 2021

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

using UnityEngine.EventSystems;
using UnityEngine.UI;

Answered by Dead Lord on January 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP