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 CB-TV · Nov 13, 2013 at 05:45 PM · variableloopwhilefor

Help With "For Loop" Not Working?

I have a "for loop" which I want to execute the code inside every time a "SpiderAmount" is added. Here it is:

 for (var i = 0; i < SpiderAmount; ++i)
 {
     Instantiate (spiderPrefab, Vector3 (2.0, 0, 0), Quaternion.identity);
     print (i);
 }

I put it in an "Update function" and it printed i forever and kept on printing. It printed 0, 1, 0, 1 constantly. I must be doing something wrong, do you know what?

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

2 Replies

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

Answer by Dracorat · Nov 13, 2013 at 05:50 PM

Well, by virtue of being in the Update function, it means you're running it every frame. So, every frame you're spawning (for now) two spiders.

Shouldn't you have some condition that causes this to happen? Like a timer or a collision?

Comment
Add comment · Show 3 · 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 CB-TV · Nov 13, 2013 at 05:53 PM 0
Share

The condition that causes it to happen is how many "SpiderAmount" there is? I put it in an "if statement" when the L mouse button is pressed but it still went on forever?

avatar image ArkaneX · Nov 13, 2013 at 07:26 PM 0
Share

In this case please include all the code, including both spider amount and button press checks.

avatar image CB-TV · Nov 13, 2013 at 07:48 PM 0
Share

It's fine now

avatar image
0

Answer by Lovelock · Nov 13, 2013 at 07:40 PM

Try adding one line inside your for loop if you want to do it this way:

 for (var i = 0; i < SpiderAmount; ++i)
 {
     Instantiate (spiderPrefab, Vector3 (2.0, 0, 0), Quaternion.identity);
     print (i);
     SpiderAmount--; // Reduce the amount since we just spawned a spider.
 }

There are definitely different approaches, but this should fix your for loop.

Comment
Add comment · Show 5 · 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 CB-TV · Nov 13, 2013 at 07:48 PM 0
Share

Thanks!!!!

avatar image Dracorat · Nov 13, 2013 at 09:24 PM 0
Share

FYI, this will only spawn half the desired number of spiders since your condition is < SpiderAmount, you have an incrementer: ++i AND you decrement the SpiderAmount: SpiderAmount-- (That last should not be there with the first two.

avatar image Lovelock · Nov 13, 2013 at 09:47 PM 0
Share

Just pasted the code into a basic example and it spawned the proper number of spiders. I even made SpiderAmount increase from user input and it continued spawning the proper number. Of course modifying the condition of a for loop inside of a running loop is not the ideal approach.

Another option is to set

 SpiderAmount = 0;

directly after the for loop.

Perhaps something like the following would be a more efficient approach altogether: (Forgive me for writing it in C#)

 void Update()
 {
 
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
     {
         SpiderAmount = 2;
     }
 
     if(SpiderAmount > 0)
     {
         SpawnSpiders();
     }
 }
 
 void SpawnSpiders()
 {
     for (int i = 0; i < SpiderAmount; ++i)
     {
         Instantiate(spiderPrefab, new Vector3 (Random.Range(-2.0f,2.0f), Random.Range(-2.0f,2.0f), 0f), Quaternion.identity);
         print (i);
     }
 
     SpiderAmount = 0;
 }

Wrap it in a nice spawning function and reset the variable once spawning is complete. :)

avatar image CB-TV · Nov 16, 2013 at 01:56 PM 0
Share

It spawns a spider every time I press the mouse down?

avatar image Lovelock · Nov 16, 2013 at 06:55 PM 0
Share

This was an arbitrary example to get it to spawn a prefab when you press the space key. In your program, you can modify the SpiderAmount variable however you like. $$anonymous$$aking the variable any non-zero value will spawn that number of spiders that frame.

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

19 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

Related Questions

Using for ... var ... in loop to access variable. 1 Answer

x=x Assignment made to same variable 3 Answers

Using for as while. 3 Answers

Can't instantiate in loop - crashes unity 1 Answer

For Loop inside While Loop won't Repeat 0 Answers


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