• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
Question by iagoccampos · Mar 09, 2015 at 03:42 PM · c#loopstopped

Loop stopped unexpectedly.

Good morning. I made a script with a short loop:

 for(int i = 1; i <= multiple.Length; i++)
         {
             Debug.Log(i);
             lastSide = sideStarted = (int)features.multipleSide[i];
             lastPosition = features.Position[i].position;
             
             for(int y = 0; y <= randSize; y++)
             {
                 if(y%2==0)
                 {
                     Random(bla);
                 }
                 else
                 {
                     if(y == randSize)
                     {
                         Random(blabla);
                     }
                     else
                         Random(blablabla);
                 }
             }
             Debug.Log(i);
         }

When the multiple.Length equals 3 the loop does not continue and does not make the second loop and I do not know why. What I'm doing wrong? I tried using "continue;" after the second "debug.log (i);" but without success. Any help will be very helpfull.

Comment

People who like this

0 Show 2
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 EvilTak · Mar 09, 2015 at 03:47 PM 0
Share

Maybe your randSize is less than zero at that time?

avatar image tigertrussell · Mar 09, 2015 at 05:32 PM 1
Share

Use the step debugger. Asset > Sync MonoDevelop Project then put a breakpoint at the top of your function and run your game from the editor. When it hits the breakpoint, repeatedly hit F10 to let it step through the code; you can use the "Locals" menu at the bottom to see the state of variables or just hover over them.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by maccabbe · Mar 09, 2015 at 04:16 PM

The most likely mistake is that there is an IndexOutOfRangeException. C# uses zero based so structures such as arrays start with the 0th element and stop at Count-1 rather than start with the 0th and stop at Count. That is why you don't see

for(int i = 1; i <= multiple.Length; i++)

since the correct way to go through an array is

for(int i = 0; i < multiple.Length; i++)

Second, make sure you are not changing the value of multiple.Length. Instead of

Debug.Log(i);

use

Debug.Log(i+" "+multiple.Length);

Comment

People who like this

0 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 iagoccampos · Mar 10, 2015 at 12:44 AM 0
Share

I know about zero based, there is a reason why I started at [1] index. But you told me to make sure I'm not changing the value of multiple.Length, stored a value to a variable I found out that for some reason the variable changes value. I'm having some problems yet, but you solved the problem of my question. Thanks

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

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I Instantiate a prefab in a list? 0 Answers

c# update loop. How to? 2 Answers

What am I doing wrong here 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