Search Unity

Input.GetKeyDown only gets first instance of key press?

Discussion in 'Getting Started' started by frasderp, May 27, 2017.

  1. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    So I'm following a rather basic tutorial and so far so good. The issue I have come up against is using Debug.Log (or print) to confirm that my key inputs are working.

    When I play this in Unity, the keypresses only work the first time they are pressed. That is, I can press Up Down Left and Right, but only once for that play 'session'. Is this expected behaviour from the code I've written, or am I missing something here? I would have thought having the script inside update() it would continue to print it?

    Appreciate any insight you can give!

    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7. public enum MoveDirection
    8. {
    9. Left, Right, Up, Down
    10. }
    11.  
    12. public class InputManager : MonoBehaviour {
    13.  
    14. private GameManager gm;
    15.  
    16. void Awake()
    17. {
    18.     gm = GameObject.FindObjectOfType<GameManager> ();
    19. }
    20.  
    21.     // Use this for initialization
    22.     void Start () {
    23.      
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update ()
    28.  
    29.     {
    30.      
    31.         if(Input.GetKeyDown(KeyCode.RightArrow))
    32.         {
    33.             //right move
    34.             gm.Move(MoveDirection.Right);
    35.         }
    36.         else        if(Input.GetKeyDown(KeyCode.LeftArrow))
    37.         {
    38.             //right move
    39.             gm.Move(MoveDirection.Left);
    40.         }
    41.  
    42.         else        if(Input.GetKeyDown(KeyCode.DownArrow))
    43.         {
    44.             //right move
    45.             gm.Move(MoveDirection.Down);
    46.         }
    47.  
    48.         else        if(Input.GetKeyDown(KeyCode.UpArrow))
    49.         {
    50.             //right move
    51.             gm.Move(MoveDirection.Up);
    52.         }
    53.     }
    54. }
    55.  
    The second piece of code that prints the key presses to console.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class GameManager : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.      
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.      
    16.     }
    17.  
    18.     public void Move(MoveDirection md)
    19.  
    20.     {
    21.         Debug.Log(md.ToString() + " move.");
    22.     }
    23.  
    24. }
    25.  
    26.  
    EDIT - I am an idiot. I tend to get stuck on trying to understand why something isn't quite working rather than work through the problem. It turns out my console was counting each line as they were repeating, rather than printing a new line each time... Terrible first post!
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You had it set to 'collapse', you mean? :)
     
  3. frasderp

    frasderp

    Joined:
    Oct 6, 2016
    Posts:
    19
    Thankyou for commenting this, just checked, exactly what I had done. Such a silly mistake.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No worries :)
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    We've all done that. But kudos to you for posting such a clear and detailed question! I can tell you're going to go far.
     
    Tset_Tsyung likes this.
  6. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    ... what Joe said...