Event System SetSelectedGameObject Error, but code still runs fine.

I’m using the following line of code to change the button selected by the event system when using a game pad:


However I’m getting the following error when it is called:

Attempting to select level_1
(UnityEngine.GameObject)while already
selecting an object

The thing is the code still runs, and the button can’t be selected by a game pad without it, but it still throws up the error.

Is this something I should be worried about? If not, can I just suppress the error and move on?

Just came across this and since no one has answered I thought I’d give my solution.

The SetSelectedGameObject function has a lock in place so it will select the first item sent to it in case of a conflict.

public void SetSelectedGameObject(GameObject selected, BaseEventData pointer)
            if (m_SelectionGuard)
                Debug.LogError("Attempting to select " + selected +  "while already selecting an object.");

            m_SelectionGuard = true;
            if (selected == m_CurrentSelected)
                m_SelectionGuard = false;

            // Debug.Log("Selection: new (" + selected + ") old (" + m_CurrentSelected + ")");
            ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.deselectHandler);
            m_LastSelected = m_CurrentSelected;
            m_CurrentSelected = selected;
            ExecuteEvents.Execute(m_CurrentSelected, pointer, ExecuteEvents.selectHandler);
            m_SelectionGuard = false;

You can use:

if( UnityEngine.EventSystems.EventSystem.current.alreadySelecting == true ){}

To check if that lock is activated and do with it what you like.

The biggest concern I have about the lock is if the item the does get selected, isn’t what you want your final selection to be (this can be problematic when working with Gamepad navigation), so I usually give my assignments a priority and write a separate function that in turn calls SetSelectedGameObject().

That error seems to throw when you select two buttons during the same frame (in my case I was using OnSelect() to push an onward button selection). If you delay the second selection until the end of the frame, the error goes away.