Buttons wont unselect via SetSelectedGameObject(null), ending up with two selected buttons

I’ve been stuck on this issue for days, I’d appreciate some help:
(I’m using the old input system with Rewired)

I’d like to unselect whatever button was selected by the mouse pointer and also by other forms of navigation, because just doing EventSystem.SetSelectedGameObject(object); wont unselect the currently selected buttons properly, so when I switch from mouse to gamepad input, I get two selected buttons; one is the button the mouse was hovered last time, the other is the first button in the scene that I set as selected button on gamepad input
when i switch to gamepad input, I do

eventSystem.SetSelectedGameObject(null);

        for (int i = 0; i < buttonObject.Count; i++) {
            var button = buttonObject[i].GetComponent<Button>();
            if (button.isActiveAndEnabled && button.interactable) {
                eventSystem.SetSelectedGameObject(buttonObject[i]);
                buttonObject[i].GetComponent<Button>().Select();
            }
        }
    }

the buttonObject is a list of buttons that I might want to select if i switch to gamepad input, there is only one of three buttons ever in the scene so I can default to selecting whichever first returns interactable, so when I switch to gamepad input the first button is always selected, but first I need to unselect whatever is currentely selected.

I’d also like to add that when gamepad input is detected, the mouse gets disabled.

the selecting of the new button works but the unselection itself doesnt. In the following example I was mousing over “Exit Game” with my mouse, then pressed a button on my gamepad to activate gamepad control, the above script runs and it sets the first button (“Play”) selected, but it wont unselect the “Exit Game” button.

Thank you

Are you using UI Toolkit? The runtime UI Toolkit doesn’t have GameObject other than the single UI Document, so setting the selected gameobject probably doesn’t do anything.

I’m using Unity UI 1.0.0 (checked in package manager) and it seems that IMGUI 1.0.0.0 is also enabled. I’m using TextMeshPro also but I’m not sure if that’s relevant.
Printing out both of these returns the relevant result:

Debug.Log("button selected object: " + eventSystem.currentSelectedGameObject.name);

Debug.Log("pointer selected object: " + pointerEventData.selectedObject.name);

I also tried putting this script as a test on the buttons that are selectable in this scene and I call their Unselect() every time I switch between mouse and gamepad input, but it doesnt result in unhighlighting the button last selected by the mouse:

public class UnSelectButton : MonoBehaviour {
   public UnityEngine.EventSystems.PointerEventData pointerEventData;

   private void Start() {
       pointerEventData = EventSystem.current.GetComponent<RewiredDeviceHandler>().pointerEventData;
   }

   public void Unselect() {
       Debug.Log("unselecting " + name);
       GetComponent<Button>().OnDeselect(pointerEventData);
   }
}

but both of them only print out the button’s name that was selected via the gamepad navigation, not on hover. It seems that the button is acting as if it was SELECTED, not highlighted, here’s an image of my setup, you can see the highlight colour is now distinctly green, the select colour is yellow:

The result is the same. The button that was previously hovered by mouse remains yellow after switching to gamepad, as per the image I posted in OP…

Any ideas please?

I’m getting desperate, I’m thinking about whether it’s possible to simulate a mouse click at a specific coordinate without moving the mouse there, to trigger the “Deselect if background is clicked” on the input module (old input system)… ?

The reason why I asked about UI Toolkit is because you put this in the UI Toolkit subforum, but you’re using UGUI, which is a different UI System entirely. The correct subforum is here.

Sorry, I must have missed that subforum! I’ll repost there. Thank you for letting me know.