Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Input.anyKeyDown not working?

Discussion in 'Scripting' started by jleven22, Jul 2, 2020.

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    203
    In my debug I see 1 and 2 but never 3.

    WaitForAnyKey = 2f

    Code (CSharp):
    1. if (deathScreenActive)
    2.         {
    3.             if (waitForAnyKey > 0)
    4.             {
    5.                 Debug.Log("1");
    6.                 waitForAnyKey -= Time.deltaTime;
    7.                 if (waitForAnyKey <= 0)
    8.                 {
    9.                     Debug.Log("2");
    10.                     if (Input.anyKeyDown)
    11.                     {
    12.                         Debug.Log("3");
    13.                         SceneManager.LoadScene(mainMenuScene);
    14.                     }
    15.                 }
    16.             }
    17.         }
    Why isn't the Input firing?

    Thanks!
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    2,560
    Where does this code live?
     
  3. APSchmidt

    APSchmidt

    Joined:
    Aug 8, 2016
    Posts:
    3,709
    Did you put it in Update()?
     
    Joe-Censored likes this.
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    151
    Is it me or there is a Logic problem with this code ?
    I mean this
    Code (CSharp):
    1. if (waitForAnyKey <= 0)
    Is the ELSE of This
    Code (CSharp):
    1. if (waitForAnyKey > 0)
    I'm almost sure that Debug.Log("2"); is not executed more than One frame... then you get out of the main IF statement.

    Sorry if i misunderstood your code/goal.



     
    PraetorBlue likes this.
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    2,560
    Yep this is totally it. You'd be hard pressed to hit your key down on this exact frame.
     
    alexeu likes this.
  6. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    203
    This was it. I created a new bool to handle the issue.

    Thank you!

    Code (CSharp):
    1. if (deathScreenActive)
    2.         {
    3.             if (waitForAnyKey > 0)
    4.             {
    5.                 Debug.Log("1");
    6.                 waitForAnyKey -= Time.deltaTime;
    7.                 if (waitForAnyKey <= 0)
    8.                 {
    9.                     Debug.Log("2");
    10.                     canPressKeys = true;
    11.                 }
    12.             }
    13.  
    14.             if (canPressKeys && Input.anyKeyDown)
    15.             {
    16.                 Debug.Log("3");
    17.                 SceneManager.LoadScene(mainMenuScene);
    18.             }
    19.         }
     
unityunity