• 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 8Eye · Nov 01, 2014 at 07:24 PM · loops

Can i restart my for loop from 0

Is it possible to restart my for loop under a certain condition? What im trying to accomplish is getting a tile that is not on the border of the map using a for loop but for some reason it is still placing the tile on the border. I thought it had something to do with the for loop not looking through the previous border index in the list, hence why i wanted it to start from 0 again. here is my code:

         for(int s = 0; s < borders.Count; s++){
 
             if(start != borders[s]){
                 
                 Debug.Log("Placing start tile at " + start);
 
                 
             }
             else if(start == borders[s]){
 
                 start = Random.Range(0,tiles.Count);
             }
 
 
         }



Also i would like to note, there is nothing wrong with the borders because i checked what all the tiles in the border list were and the match perfectly with the outside of the map.

Comment
Add comment · Show 1
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 screenname_taken · Nov 01, 2014 at 07:42 PM 0
Share

you have s < borders.Count which means that the loop will go up to one less than .Count, and not go in again. So if you put an

 if (s==borders.Count-1){
    s=0;
 }

right before the ending } of your loop, it should start over.

1 Reply

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

Answer by Nebukam · Nov 01, 2014 at 07:40 PM

The way you explain your issue is quite puzzling, could you add a bit of context ? Is it a grid, what is 'start', what borders and tiles refers to, and more importantly, what exactly are you trying to accomplish ?

That being said, ( but I don't see how it would help :s ) you can set your s variable inside the for loop :

 for(int s = 0; s < borders.Count; s++){
  
              if(start != borders[s]){
                  Debug.Log("Placing start tile at " + start);
              }
              else if(start == borders[s]){
                  start = Random.Range(0,tiles.Count);
              }

              if(s == borders.Count-1 && start == null)
                  s = 0;

          }

I would suggest using a while(){} loop instead, plus resetting the loop to 0 is not good practice and might lead to infinite loop execution if you never find your 'start'.

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 8Eye · Nov 01, 2014 at 08:07 PM 0
Share

Thanks to both of you. I never knew you could edit the variable in the for loop lol.

Im am trying my look at randomly generating a dungeon and sometimes the room was placed to far to the edge of the dungeon. Because of this i made border tiles and check if the start tile of the room is on the same index as the border. I was about to quit but unity answers saved the day. Thanks again.

avatar image Kiwasi · Nov 01, 2014 at 08:16 PM 0
Share

Just be aware that this is a potentially bad idea, and can lead to hard to debug code, or infinite loops. $$anonymous$$ake sure you save before running, an infinite loop will cause Unity to freeze. The only way out is to force quit.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

28 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

Related Questions

For Loop isn't working properly! 1 Answer

How can I fix my loop steps to the framerate? 1 Answer

Foreach loop not going through all the children 1 Answer

Continuous loop 3 Answers

Yield return inside a loop slowdown problem 2 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