Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

if statement always uses else

Discussion in 'Editor & General Support' started by poken21, Aug 5, 2021.

  1. poken21

    poken21

    Joined:
    Apr 14, 2021
    Posts:
    4
    Hi i have this simple script:

    Code (CSharp):
    1.  if (Input.GetKeyDown(KeyCode.Space))
    2.         {
    3.             Debug.Log("1");
    4.         }
    5.         else
    6.         {
    7.             Debug.Log("0");
    8.         }
    9.  
    Its should write 1 when the space bar is pressed and 0 when not pressed. But it just writes "0" What am i doing wrong?
     
    Last edited: Aug 5, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    First, if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Go read the docs for
    Input.GetKeyDown()


    1. it has to be used in Update()
    2. it only returns true on the one single frame that the key goes down.

    Are you looking for
    Input.GetKey()
    perhaps?
     
  3. poken21

    poken21

    Joined:
    Apr 14, 2021
    Posts:
    4
    Thanks for the advice i know relise how stupid it was to use GetkeyDown thanks a ton!