Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by Ninja7000 · Apr 19, 2021 at 11:14 AM · if-statementsfor-loopconfused

For loop inside if statement?

Hi, I'm kind of getting perplexed about writing an "if statements" inside a "for loop". here is the code that I don't understand.

" for (int i = 0; i < 10; i++) { if (Input.GetKeyDown(KeyCode.A)) { print(i); } }"

So what is puzzling about this is that i (which is the variable in the for loop) can be any number under 10 depending on which iteration we are on for example if we are on the 9 iteration i (which is the variable in the for loop) would be 8 right or let's say we are on the 5 iteration i would be 4 but when we are saying that if we click "A" on the keybord it only prints "0 1 2 3 4 5 6 7 8 9" but what I expected it to print was that it will print out i(which is the variable in the for loop) in that specific iteration for example lets say i (which is the variable in the for loop) is currently in the 6th iteration which means its 5 and we click "A" on the keybord it doesn't print out 5 it prints out "0 1 2 3 4 5 6 7 8 9 " do you know why this is happening? and the second thing that I'm getting confused about is lets say in each iteration in the for loop we never clicked "A" on the keybord and then the loop ends and then when I click "A" on the keybord it still prints out "0 1 2 3 4 5 6 7 8 9 " which is not what I expected because I expected it to not print out nothing because the loop has ended which means that the if statement will not work and it will not execute because the loop has ended do you have an explenation for this thank you.

Comment
Add comment · Show 6
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 Ninja7000 · Apr 19, 2021 at 11:14 AM 0
Share

do you need me to take a screenshot of the code to make it more clear

avatar image aardappel156 Ninja7000 · Apr 19, 2021 at 11:43 AM 0
Share

I would recommend posting your code with the code button

this

Also, I don't think you fully understand if statement and/or for loop (not dissing u btw). I would suggest you watch these videos which might make things clear for youif statements and for loops

avatar image Ninja7000 · Apr 19, 2021 at 11:48 AM 0
Share

ok thanks.

avatar image Ninja7000 · Apr 19, 2021 at 11:49 AM 0
Share
  for (int i = 0; i < 10; i++) {
             if (Input.GetKeyDown(KeyCode.A)) {
                 print(i);
             }
         }
avatar image Ninja7000 · Apr 19, 2021 at 12:05 PM 0
Share

Yes thank you it makes sense

avatar image Ninja7000 · Apr 19, 2021 at 07:07 PM 0
Share

I wasn't really using the loop for anything I was just testing it and seeing what it will do

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by $$anonymous$$ · Apr 19, 2021 at 11:48 AM

This is not directly connected but it is made by breakage so it will help you here is the link Url:https://youtu.be/9ozOSKCiO0I It may help you understand loops much better.

Comment
Add comment · Show 1 · 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 Ninja7000 · Apr 19, 2021 at 11:50 AM 0
Share

thank you.

avatar image
1

Answer by HellsHand · Apr 19, 2021 at 05:10 PM

Your issue is this:

A for loop processes in it's entirety in 1 frame.

GetKeyDown() is true in the frame in which it's pressed.

So in the frame that it triggers the keypress it also processes the entire loop, making every iteration of the loop share the same keypress of that 1 frame which causes it to print each iteration, because until the next frame, GetKeyDown() is true.

What I do find odd is that it always gets the key before any iterations of the loop, as opposed to printing say (5,6,7,8,9) if you happen to hit the key in the middle of it's iterations.

Basically you don't want to get key inputs during a for loop.

What is it you are trying to accomplish BTW?

A second question would be where are you running the loop? Update()? If so that is the reason you see it print, since Update() is a loop itself and means that every frame a new for loop begins to process.

Comment
Add comment · Show 4 · 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 Ninja7000 · Apr 19, 2021 at 05:23 PM 0
Share

I mean thank you that makes so much sense

avatar image Ninja7000 · Apr 19, 2021 at 05:33 PM 0
Share

Like what I'm working on

avatar image Ninja7000 · Apr 19, 2021 at 06:20 PM 0
Share

And I am running the loop in the update function

avatar image Ninja7000 · Apr 19, 2021 at 07:10 PM 0
Share

I wasn't really using the for loop for anything I was just testing it and see what it will do

avatar image
0

Answer by aardappel156 · Apr 19, 2021 at 12:01 PM

I think the input keys happens before the update function so what essentially happens is

Unity code

[insert code here that check if you pressed keyboard button]

Your code

   for (int i = 0; i < 10; i++) {
              if (Input.GetKeyDown(KeyCode.A)) {
                  print(i);
              }
          }


To maybe simply things you can imagine the input getkeydown as a boolean(i mean it is). You might understand what i mean

Comment
Add comment · Show 1 · 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 Ninja7000 · Apr 19, 2021 at 05:23 PM 0
Share

Thank you that makes so much sense

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

116 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

Related Questions

Create DoTween from for loop? 1 Answer

If statement within the For-Loop is not working - Puzzled! 1 Answer

Using i in if statement 3 Answers

For Loop Doesn't Appear to be running 1 Answer

Unity completely defys logic in this if statement. 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