exclude buttons from navigation in menu

Hello,
I have an “animated shop”-panel wich opens when I click a button. Since this panel overlaps the first layer of the main menu I disabled the buttons under it to not make them clickable anymore - works so far. But I can still navigate through them, even though the navigation should stay inside the newly opened panel. I only want to enable navigation inside the panel as long as it is opened.

This is how I made them non-interactable:

public void toggleButtonActive()
    {
        Animator animator = Panel.GetComponent<Animator>();
        bool isOpen = animator.GetBool("open");

        if (isOpen == true)
        {
            btns = GameObject.FindGameObjectsWithTag("menu");

            foreach (GameObject button in btns)
            {
                button.GetComponent<Button>().interactable = false;
            }
        }
        else if (isOpen == false)
        {
            btns = GameObject.FindGameObjectsWithTag("menu");

            foreach (GameObject button in btns)
            {
                button.GetComponent<Button>().interactable = true;
            }
        }
    }

Are there any kind of “Button-Goups” I can create and exclude from the navigation? I already have the buttons tagged as you see. Maybe I can use this tag to exclude them from navigation with mouse and keyboard?

Thank you.

Is there a parameter of a button I can set? Something similar to “button.interactable”. Maybe “button.navigatable”. But I can’t find such parameter since “button.navigation” just changes the keypress needed to navigate to the button (I don’t think its possible to assign a NULL value there).

Try adding Canvas Group to Canvas/Panel. There is also option Interactable so you can disable touch for whole panel at once.
https://docs.unity3d.com/Manual/class-CanvasGroup.html

Well, you pretty much said it. You need to access the buttons “interactable”.

let’s say your button is named, by a variable, “button”

button.interactable = false; // Can't click

button.interactable = true; // Can Click

button.interactable does not change the possibillity to navigate to the button. It only changes the ability to click it. if I only have 1 button on my shop panel I can still press the top, bottom, left and right arrow keys to navigate away from the shop button on the panel. And then the buttons under the shop overlay panel become selected (but not clicked!)

I hope you could understand me now, sorry for possible confusion.

I’m assuming you have your button('s) set up like this:

[SerializedField]private Button myButton;

Instead, you can do it like this:

[SerializedField]private GameObject myButton;

You can now use SetActive(false) to completely remove the button from the screen.
Use SetActive(true) to make it reappear.

myButton.SetActive(false); // Button is gone
myButton.SetActive(true); // Button is back

If you have a script attached to your buttons, I don’t think this is a good approach unless you don’t need the script('s) until the button is active.

Another option is to put your buttons into an array. Have an active button array and when a button is disabled, remove it from the array and add it into the inactive array. Use a foreach() to quickly SetActive(false) for each item in the inactive array. Then you can remove the button from the inactive array and set it back into the active array when you need to with the same principle.

[SerializeField]private GameObject[] myActiveButtons;
[SerializeField] private GameObject[] myInactiveButtons;

void Start()
{
foreach(GameObject button in myActiveButtons)
{
button.SetActive(true);
}

foreach(GameObject button in myInactiveButtons)
{
button.SetActive(false);
}
}

You’d really design a method though that adds/removes the buttons as needed and puts them in the proper list(and the foreach loops will be inside the method not Start). More coding than I feel like doing at the moment, for I’m half asleep right now. I hope you catch my drift here.

1 Like

Thank you, that’s a simple solution! I am wondering why I did not have this idea myself. You helped me a lot. :slight_smile:

1 Like

Glad to help, amigo! :slight_smile: