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
Answer by Hellium · May 22, 2021 at 11:54 PM
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.
Thanks! I'll look into this, but I actually solved it a different way. I switched "Navigation" from "Automatic" to "None" on that button and all other buttons.
Could I ask you to take a look at a different issue? It's the latest question I've posted. I'm trying to call a function in one script from a function in another script. Basically, Start() calls HomeScreen(), both of which are in my GameManager.cs, and HomeScreen() calls PlayMainMusic(), which is in my SoundManager.cs. I'm getting NullReferenceException.
Makes no sense at all to me, it's just a basic function call that works at first, then breaks later on. Then I scrap everything audio-related form my scripts and start over, and again, it works at first, then later gives the NullReference...
Would love to know what I'm doing wrong there!
Your answer
![](https://koobas.hobune.stream/wayback/20220613034052im_/https://answers.unity.com/themes/thub/images/avi.jpg)