UI Panel toggles off instantly. [SOLVED]

Hi people.

I have a problem with one of my UI panels.

It’s basically an inventory system. I have several items, and one of the properties of each item is a bool which defines if the item is a unique item or not. If the player picks up a unique item (using the E-key), then the GameManager executes a method, which displays a UI panel with the item icon and the item name.

I want to be able to toggle this panel off again, using the same key, the E-key.

But whenever I try to check for an E-key input to close the panel in the GameManager, it makes it so that whenever the item is picked up, the panel is activated, and then immediately deactivated.

I know why, it’s because I check for the E-key input in both scripts at the same frame, which then both enables and disables the panel (I guess).

There’s probably an easy fix for this that I just haven’t thought of, but I have kinda stared myself blind on this issue. Any chance a kind soul could help me on the right track?

The two scripts that have the conflicting behaviours are attached.

The panel is enabled my the call in line 129 in ItemInventory.cs
It is then disabled by the first if-block in the update method in _GameManager.cs

3815554–321064–ItemInventory.cs (4.04 KB)
3815554–321067–_GameManager.cs (4.52 KB)

Happends to me when i popped up a “hit any key to return to menu” after a match in my game.

When the end condition was met and the play was scrolling the map it popped back to menu without declaring a winner and whatnot

Just added a coroutine that counts down fron 3 before accepting input and returning, promt the play to get his hand off the key board

Right, but I don’t find it very responsive that the player has to wait for the system to accept a new input. :slight_smile: I want them to be able to basically deavtivate the panel at will, even if it is just half a second after having activated the panel :slight_smile:

So make it another key, a double tap or whatever you feel like

Hi
I can’t go through both scripts but it sounds like you should be using GetKeyUp in your if statement?
it could go like :

if (input.GetKeyUp(keyCode.E)){
if (isOpened) {
ClosePanel();
}
else if (!isOpened) {
OpenPanel();
}

(don’t have unity opened so this is approximate…)

Of course if you have another script operating on the same object simultaneously you should really be looking into that… Maybe you could do a quick rewrite to have both operations handled in one script, in one order, and tells the other script what to do?
Hope this helps!

No can do, sir :slight_smile: I want it to be the same button :slight_smile:

I don’t see how GetKeyUp would help me in this case :slight_smile: I want the first E-press to add the item to my inventory, and display the UI panel with its icon and name (which currently works).

As soon as I try adding the functionality to make the next E-press disable the panel, it makes it so the first E-press adds the item to the inventory, displays the panel, but then immediately closes it again.

Okay, I have made it somewhat work by putting the functionality inside only one of the scripts (which is also cleaner). The only downside right now is that for picking the item up and displaying the panel, I press the E-button. The panel stays open until I release the E-button (due to the GetKeyUp call inside the IEnumerator ActivateUniqueItemPanel).
If I make that an GetKeyDown, the panel immediately closes when it is opened, like before.

The script snippet looks like this:

    public void AddItem(Item item)
    {
        GameObject itemSlot;
        if(FindSimilarItem(item.itemID) != null)
        {
            itemSlot = FindSimilarItem(item.itemID);
        }

        else
        {
            itemSlot = FindEmptySlot();
        }

        if (item.itemStackable)
            itemSlot.transform.GetChild(1).gameObject.SetActive(true);

        itemSlot.GetComponent<ItemSlot>().hasContent = true;
        itemSlot.transform.GetChild(0).GetComponent<Image>().sprite = item.itemIcon;
        itemSlot.GetComponent<ItemSlot>().itemContent = item;

        itemSlot.GetComponent<ItemSlot>().itemContent.itemAmount += Random.Range(_itemDatabase.IDatabase[item.itemID].itemMinimumPickup, _itemDatabase.IDatabase[item.itemID].itemMaximumPickup);
        itemSlot.transform.GetChild(1).transform.GetChild(0).GetComponent<Text>().text = itemSlot.GetComponent<ItemSlot>().itemContent.itemAmount.ToString();

        if(item.itemIsUnique == true)
        {
            //ActivateUniqueItemPanel(item);
            StartCoroutine("ActivateUniqueItemPanel", item);
        }

    }

    public IEnumerator ActivateUniqueItemPanel(Item item)
    {
        uniqueItemPanel.SetActive(true);
        uniqueItemPanel.transform.GetChild(0).GetComponent<Image>().sprite = item.itemIcon;
        uniqueItemPanel.transform.GetChild(1).GetComponent<Text>().text = item.itemName;
        gameManager.PauseGame();
        readyToClose = true;
        yield return new WaitUntil(() => Input.GetKeyUp(KeyCode.E));
        DeactivateUniqueItemPanel();
    }

    public void DeactivateUniqueItemPanel()
    {
        uniqueItemPanel.SetActive(false);
        gameManager.ResumeGame();
    }

if you use keyUP for opening the menu and keyDown to close you’re promised that the user will have to let the key and and re-click it to close.

also, yes, a “InputManager” script is a ‘must’, one script to control them all if you will, make it the only one to accept input and send out commands to others, you’ll very easily get in you own way without it.

Just tried it, and the functionality is actually very, very close to what I want.

Thank you for that :slight_smile:

However, I do find it odd to pick items up by releasing a button.

Problem solved. Thank you all for your time and help :slight_smile:

Well technically you’re correct, you’re picking up by release, but if the user is just tapping the key and not holding it down he won’t tell any difference.

a lot of UI in a lot of software is actually getting the keyUP when you instinctively think it’s down because it’s when you press the key

1 Like