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:
EventSystem.current.SetSelectedGameObject(level_1);
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?
Answer by Jeremy2300 · Apr 05, 2017 at 12:33 AM
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.");
return;
}
m_SelectionGuard = true;
if (selected == m_CurrentSelected)
{
m_SelectionGuard = false;
return;
}
// 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().
Answer by gaterooze · Aug 07, 2020 at 08:10 PM
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.
Your answer
Follow this Question
Related Questions
[Still need help]Help with Error 1 Answer
Array of dictionaries: "Object reference not set to an instance of an object" 0 Answers
Errors on Script? 1 Answer
Unable to get VRTK 3.2.1 working in Unity 5.6,Unable to get VRTK 3.2.1 to run in Unity 5.6 0 Answers
Hundreds off errors n console upon starting Unity 2018.3.9f1 related to "has no meta file" 0 Answers