Why does the space bar call a function that I didn't tell it to call?

This is such a particular problem I’m not even sure how to Google it. A phone call or chat session with Unity might be necessary, but I don’t know how to get one. Here’s the situation:

.

I have several simple game states that are called in various ways. As far as my code goes, they seem to be pretty seamless. I start with HomeScreen() then click a button to call StartGame(). From there, I can either die or win. If I win, YouWin() is called and clicking a button there reloads the scene from the beginning. If I die, GameOver() is called, which has two buttons: one calls HomeScreen() and the other calls StartGame().

.

The problem is that my one battle mechanic involves spamming the space bar. When I get a game over and happen to be hitting the space bar (which is often), it will usually act as though I clicked the button to call StartGame(). There is no association between the space bar and that function at all–in fact, the space bar is ONLY ever used in my code to attack enemies. Also, this only happens within a second or two of dying–if I make sure to wait a couple seconds before hitting the space bar again as a test, nothing happens and all is well.

.

Two more observations: This doesn’t seem to be happening in the YouWin() state, only in the GameOver() state, which is odd because the circumstance is the same–I’m usually spamming the space bar to kill the “boss.” Also, I’ve coded StartGame() to stop the PlayGameOverMusic() coroutine, which, if StartGame() is mistakenly called with the space bar as it seems to be, should cancel the music, since there are no loops in that coroutine and there is a 4-second OneShot that is played before the actual game over music is established.

.

I can’t find any explanation for any of this. It appears to be a glitch beyond what my code is doing. It’s possible that it will not be like this once the game is built and published, but I’d like to address it before that if possible.

.

Thanks so much for reading and responding!

.

Steve

UI elements can be triggered from the keyboard, for accessibility reasons I think.

If you take a look at the EventSystem gameObject in your scene, you’ll see that the Standalone Input Module component defines a Submit button. When pressed, this Submit button simulates a click on the currently selectedd UI element (button / toggle / input field / …). In your case, the button calling StartGame is most likely the selected one.

In the Input Manager, this Submit button is associated to the Return key, the Enter key and the space key, causing your issue.

Remove the space key from the Submit button in your Input Manager.