Search Unity

Input.anyKeyDown not working?

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

  1. jleven22

    jleven22

    Joined:
    Mar 26, 2019
    Posts:
    421
    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:
    7,909
    Where does this code live?
     
  3. Deleted User

    Deleted User

    Guest

    Did you put it in Update()?
     
    Joe-Censored likes this.
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    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:
    7,909
    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:
    421
    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.         }