[SOLVED]Last selected button

Hi everyone,

I am trying to make a one button-press menu using a gamepad and I want when the user clicks on a button in the menu, then that button performs its corresponding action and the menu closes down.

If the user then presses the gamepad button then the menu reappears.
What I want is for the last pressed menu item to be highlighted. I have already tried with the eventSystem.lastselectedGameObject. This works as I can realize that the menu item is selected but not highlighted.

Any suggestions?

eventSystem = GameObject.FindGameObjectWithTag("EventSystem").GetComponent<EventSystem>();

selectedButton = eventSystem.currentSelectedGameObject;                      
           
eventSystem.SetSelectedGameObject(selectedButton.gameObject, new BaseEventData(eventSystem));

Solved

I replace the above code with the following:

eventSystem = GameObject.FindGameObjectWithTag("EventSystem").GetComponent<EventSystem>();
ExecuteEvents.Execute<ISelectHandler>(eventSystem.currentSelectedGameObject, new BaseEventData(eventSystem), ExecuteEvents.selectHandler);
2 Likes

Just figured this out:

public EventSystem eventSystem; //select event system in editor
public GameObject lastSelectedGameObject;
private GameObject currentSelectedGameObject_Recent;

void Update() { 
   GetLastGameObjectSelected();
}


private void GetLastGameObjectSelected() {
            if (eventSystem.currentSelectedGameObject != currentSelectedGameObject_Recent) {

                lastSelectedGameObject = currentSelectedGameObject_Recent;

                currentSelectedGameObject_Recent = eventSystem.currentSelectedGameObject;
            }
 }
1 Like