Clicking off button causes selected button to become deselected

The video below demonstrates an issue I’m having. I’ve set it up in a way so that when either the arrow keys or WASD are moved around it selects a button (the buttons in this case are weapons from your full inventory) or you can select the weapon by simply clicking it. This in turn, sets a boolean to true in the script, as you’ll see in the video (the bool being called weaponChosen). However, if I click off any of the buttons, anywhere (even something that’s not a button, just an image UI for example) it deselects the last button that was selected. I wondered if there was a way to have it not deselect anything unless you’ve clicked another button (aka a weapon)

enragedmelodicimperialeagle

Thanks guys :slight_smile:

The way I do this is to watch what is selected and if there ever isn’t one of the things that I decide MUST always be selected, I reselect the last one that I had observed selected in that list, eg, in the previous frame.

You can select stuff via the EventSystem: and I believe it’s this call:

Note: it is an instance method, not a static… use the .current shortcut

1 Like

Please don’t use the 2D forum for UI support. There’s a dedicated UI forum here where I’ll move your post.

Thanks.

Interesting, I’ll have a look at this tonight, so yeah, I guess at that point I know that at least 1 of the 4 of the weapons MUST be selected.

so essentially if EventSystem.current.currentSelectedGameObject != to any of the 4, select the previous selected. Previously selected could be stored in a variable. But yeah will look at tonight, thanks for the idea :slight_smile:

Ah sorry about this Melv! Totally slipped my mind, thank you for moving :slight_smile:

1 Like

imperfectterrificacornweevil

Got it working :slight_smile: So now if you click anywhere else it doesn’t deselect it. Thank you again for the suggestion @Kurt-Dekker .

Here is my code if you’re interested. I had the GameObject array turned into a List, which I could then use the Compare method on. So I checked to see if the selected GameObject was in the List of weapon selections, if not, then it selected the previously selected. I also had to do the same for the two buttons on the page - the Cancel and the Swap buttons, if I didn’t, it wouldn’t let me select them and execute their functions.

public class WeaponSwapButtonLogic : MonoBehaviour
{
    public GameObject firstWeaponSelected;
    public List<GameObject> weaponButtons;
    public List<GameObject> cancelAndSwapButtons;
    public GameObject lastSelectedButton;
   
    void Start() {

        emptyAndSetSelectedGameObject(firstWeaponSelected);
    }
   
    void Update() {

        GameObject selectedGameObject = EventSystem.current.currentSelectedGameObject;

        foreach (GameObject weaponButton in weaponButtons) {

            SwapButtonLogic swapButtonLogic = weaponButton.GetComponent<SwapButtonLogic>();

            if (weaponButton == selectedGameObject) {
                swapButtonLogic.weaponChosen = true;
                lastSelectedButton = weaponButton;
            }
            else {
                swapButtonLogic.weaponChosen = false;
            }

            if ( ! weaponButtons.Contains(selectedGameObject) || ! cancelAndSwapButtons.Contains(selectedGameObject)) {

               emptyAndSetSelectedGameObject(lastSelectedButton);
            }
        }
    }

    void emptyAndSetSelectedGameObject(GameObject gameObjectToSelect) {

        EventSystem.current.SetSelectedGameObject(null);
        EventSystem.current.SetSelectedGameObject(gameObjectToSelect);
    }
}
1 Like

Hey there, I’ve been looking for a solution to this problem for a while now, the above does work wonders, but I was trying to avoid this mostly because I didn’t want the UI to really have anything in its Update function.

In my case there are wrapping *Screen components (MainMenuScreen, AchievementsScreen etc.) which can have multiple buttons inside them, so all it is doing in the Update function is looping through the buttons registered to that screen and checking whether at least one is selected and selecting the last selected or default button if there aren’t any.

This doesn’t seem to have any performance impact really, but is my apprehension justified for this? I’m fairly new to Unity so don’t fully have a grasp of what’s expensive and what’s not yet, so just putting the question out there to anyone more familiar with it.