OnPointerExit not updating if container panel was SetActive(false)

Hello

In my main menu, while using Ui components on a Canvas, I transition from a set of main options (start, settings, exit) to a settings panel - same Canvas, just a different panel.
Upon clicking the “Settings” button, I set the main panel as SetActive(false) and enable the settings one just by doing the opposite.

My problem is with the “hover” state of the buttons. In each individual panel, while active, OnPointerEnter and OnPointerExit work fine, but if I transition panels, the state doesn’t update, such that when I close the settings panel, the “Settings” button is stuck on the OnPointerEnter state, it never received a OnPointerExit update.

Is there something I am missing or any way I could force the update of the button after the panel was SetActive(false)?

I found an answer after fiddling with the event system:

    public void OpenSettings() {
        panel_Settings.SetActive(true);
        var pointer = new PointerEventData(EventSystem.current);
        ExecuteEvents.Execute(EventSystem.current.currentSelectedGameObject, pointer, ExecuteEvents.pointerExitHandler);
        panel_MainOptions.SetActive(false);
    }

It feels like a hack, but it works both ways. Just need to make sure I don’t disable the previous GameObject until I execute the pointer event. Yup, hacky. Any other suggestion?

Thanks for posting that. I hadn’t thought of doing it that way. Here’s another way that doesn’t require the object to be selected. Run the OnDisable() method on your monobehavior and pass the rectTransform into this function to see if the mouse is over it. If it returns true, then you can call the exit handler.

public static bool MouseInRect(RectTransform rectTransform) {
            Vector2 localMousePosition = rectTransform.InverseTransformPoint(Input.mousePosition);
            if (rectTransform.rect.Contains(localMousePosition)) {
                return true;
            }
            return false;
        }