How navigate with gampad in the inventory

Hi
I trying to navigate with the gamepad in the Inventory and select them too.
For that I create a button as a child of InventorySlot prefab.
But I can’t navigate or select one of this buttons.
If I have open my game menu together with the inventory, then I can navigate through the inventory, and can select the button to drop an item for example, but for sure this is not what I want.

Every hint would make me very happy!!!

I would put the button component directly on the InventorySlotUI GameObject. The big question is what you are trying to achieve with the buttons. This will help greatly in guiding the button’s use.

Hi Brian
The Buttons are just for testing the navigation in the UI. In the end I want navigate trough the inventory with the gamepad/keyboard. At the highlighted item in want to can open a dropdownmenu to: drop, equip or inspect the item.

I spend many times, hours, to understand the new Input system. There are different concepts.
They looks logical. But knowhere I found a good explanation about, deep inough when it’s more complex. Now I know that I know nothing how the Input system really works.
I think I know the Input system good, but I have completely chaos inside my brain.
I have many many courses from GameDev.tv and I love them!!!
Is there a chapter where explane Input UI and PlayerInput with gamepad I didn’t found?

Update:
Ok Now I understand the Input System better. I need alway a first Button.
Back to the Inventory. I Updated the InventoryUI script.

private void Start()
        {
            Redraw();
            AddButtonsInList();
        }

        private void Redraw()
        {
            foreach (Transform child in transform)
            {
                Destroy(child.gameObject);
            }
            for (int i = 0; i < playerInventory.GetSize(); i++)
            {
                var itemUI = Instantiate(InventoryItemPrefab, transform);
                itemUI.Setup(playerInventory, i);
            }
        }

        private void AddButtonsInList()
        {
            for (int i = 0; i < playerInventory.GetSize(); i++)
            {
                inventoryButtons.Add(new GameObject());
                Debug.Log("Slot number " + i);
            }
            Debug.Log("First button " + inventoryButtons[0]);
        }

        public GameObject GetFirstSlot()
        {
            return inventoryButtons[0];
        }

Then I Get the first Slot GameObject where I handle my UIs.

var eventSystem = EventSystem.current;

                eventSystem.SetSelectedGameObject(inventoryUI.GetFirstSlot());

But it gives me this error:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.

And sorry I have still problem to paste the code in a good format here.

Greetings
Phaidon

You were very close. The ``` are the ` that is next to the 1 in the upper left of the keyboard, not the ’ between the ; and the return key. I’ve edited the post to make it readable.

My best guess here is that the call to SetSelectedGameObject.inventoryUI.GetFirstSlot());is happening before the first draw has occurred…

This is actually only creating a list of empty GameObjects, which… really does nothing…
Here is how I would rewrite this, eliminating the AddButtonsInList() method altogether:

private void Redraw()
{
    foreach(Transform child in transform)
    {
          Destroy(child.gameObject);
    }
    inventoryButtons.Clear();  //must start with a new list each refresh, old list is invalid
    for(int i=0;i<playerInventory.GetSize();i++)
    {
          var itemUI = Instantiate(InventoryItemPrefab, transform);
          itemUI.Setup(playerInventory, i);
          inventoryButtons.Add(itemUI.gameObject);
    }
    //If the inventory was redrawn, then the SelectedGameObject has likely been destroyed
    EventSystem.current.SetSelectedGameObject(inventoryButtons[0]);
}

Hi Brian
Your code works super! Thank you so much!!
Only one smal thing, I had to make the Redraw() methode public and invoke it always when I open the Inventory. Otherwise after I open up the first time the inventory, then, it doesn’t works anymore,the invetory don’t redraw.
But now it’s perfect! :slight_smile:

I have to use the english keyboard layout not the swiss one :wink: For next time…

Greetings
Phaidon

Unity’s pre-defined keycode definitions are very Angelo-centric, something all too common in nearly all aspects of code development. While that works out great for me, it’s something that can throw people with any of the other 79 other keyboard layouts Windows recognizes off.

The Angelo focus definitely makes sense.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms