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 /
avatar image
0
Question by Hakimo · Jun 18, 2012 at 09:45 AM · whileforloop

Inconsistency with For Loop

Hi,

I have a question regarding for loops. Here's a snippet of code:

 public var heart : GameObject;
 public var maxHeart : int;
 public var i : int;
 
 function Update() {
   for (i=0; i<maxHeart; i++) {
      instantiate(heart,Vector3(i*20,0,0),Quaternion.Identity);
   }
 }


In the inspector, I set the maxHeart to 4. When I click play the i stops at 4 but instantiates non stop in the scene. Why is it behaving like that when the condition of the loop is if it's less than maxHeart?

So I replace the for loop with while loop instead:

 while(i < maxHeart) {
   i++;
   instantiate(heart,Vector3(i*20,0,0),Quaternion.Identity);
 }

and this bloody works. I would like to know why the for loop is behaving like this when both of the functions are similar.

Comment
Add comment · Show 4
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 Mizuho · Jun 18, 2012 at 09:48 AM 1
Share

The for loop sets "i=0" at the beginning all the time. In the while loop, this doesn't happen, so i is always larger than maxHeart when the loop is reached again.

avatar image Eric5h5 · Jun 18, 2012 at 09:53 AM 2
Share

As a side note, it's better practice if you keep variables local where possible. Don't declare "i" outside the functions it's being used in.

avatar image Mizuho · Jun 18, 2012 at 09:56 AM 1
Share

Eric is right. What you would normally do for something of the nature you want is use this code in Start() because you want them instantiated at the beginning, but only once.

avatar image Hakimo · Jun 18, 2012 at 10:08 AM 0
Share

Thanks very much guys. Learned something new today :)

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Jun 18, 2012 at 09:53 AM

The for loop has 3 parts:

 for (PART1; PART2; PART3)

PART1 is executed one time when the for loop is entered, this is usually used to initialize a variable like you did.

PART2 is a condition which is checked reight before each iteration. If it's false the for loop is terminated.

PART3 is executed right after each iteration. It's used to prepare the next iteration step.

You problem is that you set the variable i to 0 each time you execute the code (which is every frame since you have it in Update).

Btw, You really shouldn't use a public instance variable as for loop variable. for loops usually use local variables. Also the name "i" is ok as local temporal variable in a vor loop, but as instance variable the name doesn't make much sense.

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 Hakimo · Jun 18, 2012 at 10:07 AM 0
Share

Thanks very much. I thought the initialisation/part 1 is always called once. Didn't know function update resets it to 0 everytime. Also, the reason we declared the vars outside of the scope is so we can change it in the inspector. If we do it local, does that mean we have to hard code the values? Is it not good practise if we change it at the inspector?

Thanks again :)

avatar image Wolfram · Jun 18, 2012 at 10:43 AM 2
Share

Note you can also "cheat" by leaving any of the PARTs empty. But that's rather hackish:

 for (; i<maxHeart; i++) // skip initialization

 for (;;) // infinite loop
avatar image Bunny83 · Jun 18, 2012 at 11:04 AM 1
Share

@Wolfram: It's not even cheating ;) It has those three sections, what you do with it is up to you. It's also possible to iterate through a linked list like this:

 for (item = first; item != null; item = item.next)
avatar image Bunny83 · Jun 18, 2012 at 11:14 AM 1
Share

@Hakimo: No, you got me wrong. maxHeart should be declared outside. It also has a good name. I was talking about the variable "i". It doesn't have any meaning outside of the forloop and also doesn't need really need to keep it's value.

If you want to execute the for loop once, you should call it in Start() and not in Update ;) It makes no sense to execute the for loop every frame with a condition that should only be true for the first frame.

avatar image
2

Answer by whydoidoit · Jun 18, 2012 at 09:49 AM

Because the for loop is setting i back to 0 every Update and the while loop isn't.

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

How to spawn a GameObject by name? 2 Answers

Do something while key is pressed and held down. 1 Answer

How do I call a script or a function to be active 'while'a GUITexture is displayed? 1 Answer

Using while statements 0 Answers

apply something on enum 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