Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm learning Unity but my key press isn't being detected

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
    public Rigidbody myBody;
    private float time = 0.0f;
    private bool isMoving = false;
    private bool isJumpPressed = false;
    void Start(){
        myBody = GetComponent<Rigidbody>();
    void Update()
        isJumpPressed = Input.GetKeyDown(KeyCode.Space);
        Debug.Log(isJumpPressed);
    void FixedUpdate(){
        if(isJumpPressed){
            myBody.velocity = new Vector3(0,10,0);
            isMoving = true;
            Debug.Log("jump");
        if(isMoving){
            time = time + Time.fixedDeltaTime;
            if(time>10.0f)
                //Debug.Log( Debug.Log(gameObject.transform.position.y + " : " + time));
                time = 0.0f;

why isJumpPressed always false. What am I doing wrong? From what I understand this should work but I'm obviously missing something

UPDATE: Thanks to everyone who proposed ideas. I got the isJumpPressed to return true when I stopped trying to detect the space bar.

isJumpPressed = Input.GetKeyDown("a");

anyone got any ideas why this would work and not

isJumpPressed = Input.GetKeyDown(KeyCode.Space);
isJumpPressed = Input.GetKeyDown("space");

UPDATE 2: Apparently this is a bug in Linux. I've read it won't happen when the game is built just in the editor. I've found a workaround at https://forum.unity.com/threads/space-not-working.946974/?_ga=2.25366461.1247665695.1598713842-86850982.1598713842#post-6188199

If any Googler's Stumble upon this issue reference the following code because this is working for me.

public class Player : MonoBehaviour
    public Rigidbody myBody;
    private float time = 0.0f;
    private bool isMoving = false;
    private bool isJumpPressed = false;
    void Start(){
        myBody = GetComponent<Rigidbody>();
    void Update()
        isJumpPressed = Input.GetKeyDown(SpacebarKey());
        if(isJumpPressed)
            Debug.Log(isJumpPressed);
    void FixedUpdate(){
        if(isJumpPressed){
            myBody.velocity = new Vector3(0,10,0);
            isMoving = true;
            Debug.Log("jump");
        if(isMoving){
            time = time + Time.fixedDeltaTime;
            if(time>10.0f)
                //Debug.Log( Debug.Log(gameObject.transform.position.y + " : " + time));
                time = 0.0f;
    public static KeyCode SpacebarKey() {
        if (Application.isEditor) return KeyCode.O;
        else return KeyCode.Space;
                This is a strange case, and your code should work. I looked and found that you might want to try to disable and re enable the game object.
– ChilliPenguin
                Aug 29, 2020 at 16:20
                Hi y'all thank you for the comments. I got the code to work when I do isJumpPressed = Input.GetKeyDown("a"); I dont understand at all why its not detecting my space bar. Anyone got any ideas?
– jakegergen
                Aug 29, 2020 at 19:49

The problem is that FixedUpdate and Update are not called one after the other. Update is called once per frame and FixedUpdate is called once per physics update (default is 50 updates per second).

So the following could happen:

Update is called -> GetKeyDown is true (this frame only) ->  isJumpPressed = true
Update is called -> GetKeyDown is false ->  isJumpPressed = false
FixedUpdate is called -> isJumpPressed is false 

Here is an example that prints "Update Jump" every time you press space, and "FixedUpdate Jump" only sometimes when you press space. Do not do this:

bool isJumpPressed;
void Update()
    isJumpPressed = Input.GetKeyDown(KeyCode.Space);
    if (isJumpPressed)
        Debug.Log("Update Jump");
private void FixedUpdate()
    if (isJumpPressed)
        Debug.Log("FixedUpdate Jump");            

Do this instead:

bool isJumpPressed;
void Update()
    if (Input.GetKeyDown(KeyCode.Space))
        isJumpPressed = true;
        Debug.Log("Update Jump");
private void FixedUpdate()
    if (isJumpPressed)
        Debug.Log("FixedUpdate Jump");
        isJumpPressed = false;
                @jakegergen Just in case: make sure the script is attached to a gameobject and enabled, you are in playmode and the game window is in focus.
– Pluto
                Aug 29, 2020 at 18:26
                @jakegergen Even with the spacebar key fix you will still have the issue mentioned in my answer, so you should change your code.
– Pluto
                Aug 29, 2020 at 20:15

One possible problem is that your project might be using the new Input System package. If you are using this package, the old Input Manager functions will not work. To check this, go to Edit > Project Settings... > Player > Other Settings and Active Input Handling should be set to Input Manager (Old) (or Both might also work).

If you actually want to use the Input System package, you have to install it if you don't already have it, and you would check if the spacebar is pressed like so:

using UnityEngine.InputSystem;
jumpPressed = Keyboard.current.space.wasPressedThisFrame;
                Thank you for your input I didnt consider this. However my input handling is using the old Input Manager
– jakegergen
                Aug 29, 2020 at 19:41

I had the same issue. Spend some hours trying to figure out. Maybe all was good from the beginning until I figured out. When you I executed the play to see the result and test the movement, by mistake I chose the scene tab. If I want to see the result (to play) I must click the "Game Tab". In Game Tab the keys were detected.enter image description here

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.