- Home /
button.Select() and alternatives don't seem to work when called via new input system
Hello,
I've got a weird problem. I've been trying to make a map for my game, which also includes a list of relevant objects on the map that can be clicked (it's all Buttons).
Now the intention is that if you click something on the list it's marked and focussed on the map and if you click something on the map it's marked and selected on the list (for better controller support).
The first part works perfectly fine. Where things get weird is when I click something on the map. For some reason the Button does not get selected. Actually nothing gets selected despite the script being successfully run (put some random button on the UI and tried to select it with GameObject find). Meanwhile when I run the same select script from a UI Button that works perfectly fine.
The only real difference I can find is that when the script doesn't work it's called from the new InputSystem.
controls.playerControls1.Interact.performed += ctx => mapClick();
While the other time it's triggered directly by the button.
private void OnEnable() {
Button.onClick.AddListener(onClick);
}
I've been using this standard procedure to set the selected button:
public static void SetSelectedUIElement(GameObject _gameObject)
{
UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null);
UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(_gameObject);
Debug.LogWarning("Menu: SetSelectedGameObject to: " +_gameObject.name);
}
I also tried something like button.Select(), but that also fails.
I'm extremely confused why I can't set the SelectedGameObject via my input system, while it works perfectly fine for the button click. It does seem to sort of work, because the button flickers white for a moment, even on the mapClick() even if it doesn't select. (That doesn't happen when I comment out the selection code, so the selection Code seems to be doing something).
Did anyone else ever experience something like this?
Edit: Hm, I've experimented a bit more and it doesn't seem to be the input system per se, it's only when clicked via mouse. So I suspect the script works, but the mouseclick unsets the button immediately afterwards since I clicked somewhere else on the UI, not the selected button. Does anyone know how to deal with this?
Answer by Omti1990 · Feb 05, 2021 at 03:11 PM
Okay found the solution for my problem:
I just needed to remove the tick on DeselectOnBackgroundClick in the InputSystem UI Input Module on the EventSystem gameObject in the inspector.
It works perfectly now...
sorry for the confusion, it really seemed like the code wasn't working and I didn't realize the mouse click was the problem.
Your answer
Follow this Question
Related Questions
Touch a specific area on the screen 2 Answers
Second Player Cannot Navigate the Menu 2 Answers
Unity sometimes misses Mouse Button Release 1 Answer