Cannot get OnPointerEnter to work, please help

Hi, I am having real trouble with getting my mouse to change the currently selected button to whichever button is currently highlighted.

The reason is that I use gamepads as well as mouse / keyboard, and the UI is set to make the first button active. However, if you hover over another button with the mouse, you have two highlighted buttons.

I am trying to use the following code, but it simply doesnt work.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MouseSelector : MonoBehaviour, IPointerEnterHandler {

    public void OnPointerEnter(PointerEventData eventData)
    {
        EventSystem.current.SetSelectedGameObject(eventData.pointerCurrentRaycast.gameObject);
    }
}

This should work, but the OnPointerEnter event never triggers, even if I just chuck a debug.Log hello in there.

The mouse must be working as the buttons highlight, I simply cannot hook into that and use it.

Any ideas / thoughts?

I have a fix, by attaching the following code to each button

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


[RequireComponent(typeof(Selectable))]
public class HighlightFix : MonoBehaviour, IPointerEnterHandler, IDeselectHandler
{
    public void OnPointerEnter(PointerEventData eventData)
    {
        if (!EventSystem.current.alreadySelecting)
            EventSystem.current.SetSelectedGameObject(this.gameObject);
    }

    public void OnDeselect(BaseEventData eventData)
    {
        this.GetComponent<Selectable>().OnPointerExit(null);
    }
}

What I dont understand is why my solution above doesnt work…

1 Like

Dude! I had a kind of different issue, but your code fixed mostly everything! Thank you!

Also, this thread is old. Sorry. :smile: