1
\$\begingroup\$

I've been working on a top down 2D game for a while now, and yesterday I found a strange bug that I just can't explain to myself whatsoever. I have a loot window for looting enemies, as well as a character panel to equip gear/see stats. I realized that after I open and close a loot window, if I press the space button (used for attacking, not for opening any windows) while holding A/LeftArrow or W/UpArrow it opens the character panel. Once this happens, I can press space to open/close the character panel.

Went to the character panels OpenClose() function (all it does is set the canvas group alpha to 0 and block raycast to false and vice versa) which is being called unintentionally, put a debug.log inside to verify if it really was being called, and yes, it is. I look up all references to see where I used it, but I only use it in a single place in the project that is behind an if-statement looking for they keycode C (NOT space). I added a debug.log for the Input.inputstring to see if somehow a magic C button press is ending up in that function, but no. If I press C to open the window, the debug log pops up, if I press space, the inputstring appears to be empty, so the if statement to get to the only place in the code referencing that function cannot be met. Removed the space button from my Player entirely, the behaviour still persists.

Added another debug.log with stackTrace.GetFrame(1).GetMethod().Name to show who is actually running this function, but it turns out that if I run it and press C, it says it's being opened by the update function in the UIManager (as expected). If I run it the strange, unintended way, it says it is being run by the EventSystems Invoke function. Coupled with the fact that the behaviour persists despite the space button being removed from the player, I realized that it's Unity's built in "Submit" button being pressed.

Strange behaviour:

  • It appears to call the submit function on the OpenClose button for the character panel, but only under the circumstance that I closed a loot window and haven't clicked the mouse anywhere yet afterwards. If I deactivate the button that holds the character panels OpenClose function, the behaviour stops. The button shouldn't be pressed though, because it is on a canvas group with alpha 0/block raycast=false, just like all the other buttons with the identical function that work fine. The loot window has no idea about the character panel either, and all the windows are properly wired to their own OpenClose function.
  • Upon further inspection, the window only starts showing up if, after closing a loot window, i press A/LeftArrow+Space or W/UpArrow+Space. This is getting more and more confusing for me.

So basically my issue is; how do I figure out why this function is being called? It feels as if closing the loot window somehow "caches" the OpenClose button for the character panel onto the submit button, but only until I click elsewhere on the screen. Did you guys have any experience with a similar situation? Could you share some pointers on how to debug this? What is the "submit" button usually used for, and what would make it only work in combination with the left/top directional buttons? I've spent about 4 hours on this now and don't know how to get any further.

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

First, I would recommend looking at the debug view of the EventSystem GameObject in the inspector window. Looks like this:

1

I’m guessing that your button is still the selected GameObject (the very first row in the debug window) even though it’s invisible, so input events are still being sent to it, including the standard “spacebar presses buttons” UI event (configurable in several places). Just like the OS UI system, clicking an interactive element selects it, causing it to be the active, or focused, control. When you click away, or navigate using other methods (eg. pressing tab, arrow keys, etc.) then a different element or no element will become selected.

If you don’t want to allow the keyboard to be used to allow UI focus navigation, you can turn off focus navigation on a per-object basis by selecting the GameObject, finding the Navigation dropdown, and choose “None”. Then that object will never become selected, so pressing spacebar will not click the button. It will still be interactive though, so clicking will still work.

If you do still want to allow focus navigation via keyboard, then you need to actually remove focus from the GameObject. This can be done by disabling the GameObject, or by calling

UnityEngine.EventSystems.EventSystem.current.SetSelectedGameObject(null);

Once you no longer want the keyboard to activate the UI

\$\endgroup\$
1
  • \$\begingroup\$ Thank you very much for your input. Strangely, the selected GameObject in the window you mentioned remains the OpenClose() function of the loot window, not of the character panel. Adding a debug.log into the function that remains selected only outputs once (when I initially close the loot window), after that the space button opens the other window. It still confuses me why this only works after opening the loot window, not before. The solutions work though, so I guess I'll just disable navigation now. I'll leave the question open and accept it in a few hours if noone else trumps it. Cheers! \$\endgroup\$
    – DavidK
    Dec 15, 2019 at 18:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .