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
Right, but I don’t find it very responsive that the player has to wait for the system to accept a new input. 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
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!
I don’t see how GetKeyUp would help me in this case 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.
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.
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