Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CrazyyyLowy · Jun 10, 2020 at 05:12 AM · beginnerelse if

Absolute beginner, else if

Hey Guys I am an absolute beginner just started unity, and I learned about if and else if, check this code

 // Update is called once per frame
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.UpArrow))
         {
             Debug.Log("UpArrow is Pressed");
         }
         else if (Input.GetKeyDown(KeyCode.DownArrow))
         {
             Debug.Log("DownArrow is Pressed.");
         }
         else if (Input.GetKeyDown(KeyCode.Return))
         {
             Debug.Log("Enter is Pressed.");
         }
 
     }
 }


So if I understand correctly, when I press the UpArrow and DownArrow at the same time, then just one of the messages should appear, but thats not the case when I tested it, if I press both of them (or even all of them, doesn't matter) then both messages appears in the console, what am i doing wrong? is it about Update()? idk Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JavaMcGee · Jun 10, 2020 at 06:23 AM

Input.GetKeyDown is only true for one frame in which is the key is pressed, and Update is called every frame. Your code is logging both keys because you're pressing them a split second apart, in different frames. Use Input.GetKey instead to see something more like what you expect (but will print output repeatedly while key is pressed).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CrazyyyLowy · Jun 10, 2020 at 07:18 PM 0
Share

Hey, thanks for the answer, I saw a video where the guy in there uses Input.GetKeyDown, and it works just fine,I am pretty sure he was using an older version of unity,have they changed something in Update in the current versions, let me know.

avatar image streeetwalker CrazyyyLowy · Jun 10, 2020 at 08:26 PM 0
Share

GetKeyDown works just fine, but you misunderstood the IF ELSE construct because of how GetKeyDown behaves.

Because it is virtually impossible to press two keys down on exactly 1 frame, that is why Java$$anonymous$$cGee's explanation is correct. You're getting one key logged on one frame and the other on the next frame.

GetKeyDown only fires (is true) for exactly 1 frame and no more, even if you continue to hold the key down.

On the other hand, GetKey remains true as long as a key is held down.

Using GetKey ins$$anonymous$$d of GetKeyDown, If you press two keys at almost same time (again, next to impossible that that you will hit both keys on the same frame) You will see one key logged, and from there after whichever key is above the others in your IF-ELSE chain will be logged and no other key will be logged.

So for your example, if you press UP Arrow and keep it held down, and then press Down Arrow and keep it held down, you will only see "UpArrow is Pressed", because the UP Arrow test comes before the Down Arrow test in your IF ELSE construct.

And, if you press Down Arrow and keep it held down, and then on the next frame press UP Arrow and keep it held down, you will see "DonwArrow is Pressed" for one frame, and from there after you will see "UpArrow is Pressed", again because the UP Arrow test comes before the Down Arrow test in your IF ELSE construct.

It will prove that your IF ELSE behaves like you think it should.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

141 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

standard Assest 1 Answer

How to substitute one object with another in animation clip 0 Answers

Why is my Unity different than the one in the Beginner Tutorial? 1 Answer

2D trigger and mouse button help 0 Answers

My raycasting isn't working 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges