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 GameSlayerGS · Oct 03, 2013 at 01:38 PM · loopwhilefor

For Loop inside While Loop won't Repeat

I've been working on a smallish bit of code for several days now, I think I've finally gotten it working just the way I want, except for one thing. It relies on a very essential bit of code that's inside of a For Loop, which is set to repeat once for every item in an Array. That For Loop is inside of a While Loop, which is set to repeat until a certain variable equals another variable, one variable is defined beforehand but the other is changed by the For Loop. The contents of the While Loop repeat, except for the For Loop inside the While Loop, which only runs the first time.

     while(true /*workingNumber != target1Number*/){
         yield WaitForSeconds(0.001);
         for(var value : float in numbersList1){
 //            print("1");
             randomNumber = Random.Range(0, (dup1.length));
 //            print("2");
             workingNumber = (workingNumber + dup1[randomNumber]);
 //            print("3");
             combination.Push(dup1[randomNumber]);
 //            print("4");
             dup1.RemoveAt(randomNumber);
             print("5");
             if(workingNumber == target1Number){
                 print("Success!");
                 solution = combination;
                 print(solution.join(", "));
                 solutionString = (solution.join("\n"));
             }
             else {
                 print("Failed.");
             }
             yield WaitForSeconds(0.001);
         }
         workingNumber = 0;
         print("Trying again.");
         dup1 = numbersList1;
     }

Pardon the uncreative variable names. Also, the While Loop, as you can see, is set to just go on indefinetely, I just changed it to try to figure out the problem, the two variables are in the comment there. If this isn't enough, let me know, I'll put up the rest of the code. Also, the excessive "print" commands were for helping figure out what was going wrong, they're commented out because I determined I didn't need them.

Comment
Add comment · Show 17
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 robertbu · Oct 03, 2013 at 02:39 PM 0
Share

Strange. Do you also have this problem if you change the "for-each-type-loop" to one that runs with an index?

 for (var i = 0; i < numbersList.Length; i++) {
avatar image GameSlayerGS · Oct 03, 2013 at 03:16 PM 0
Share

Not exactly, but I do get a different problem, I changed your code bit to:

 for (var i :  int = 0; i < arrayLength; i++) {

And then replaced my For Loop with that, (arrayLength is a variable that was previously set to numbersList1.length). I made that change and added specifying var i as int, which cleared up another error, I don't know which change fixed it because I made them both at the same time. Now I get:

 ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
 Parameter name: index
 0
 System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) (at /Applications/buildAgent/work/c514da0c8183631c/mcs/class/corlib/System.Collections/ArrayList.cs:3261)
avatar image Jamora · Oct 03, 2013 at 04:22 PM 1
Share

You say you operate on copies, but are you really? (cough line26 cough)

avatar image robertbu · Oct 03, 2013 at 04:41 PM 1
Share

You need to research 'by value' and 'by reference'. In the case of Array doing:

 dup1 = numberList1; 

makes both variables point to the same list. Any changes made in the information pointed to by either variable will be reflected in both variables.

P.S. Consider using a generic list rather than an Array. That won't solve this problem, but it might make some things easier.

http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?

avatar image Hoeloe · Oct 03, 2013 at 04:59 PM 1
Share

robertbu is right here. That doesn't copy the list, it copies the reference to the list. For performance reasons, complex datatypes are not directly referenced in your code. Ins$$anonymous$$d, they have a proxy, or reference, which is just the memory address pointing to the data structure. When you operate on the object, the program follows the reference to the data, and operates on that. When you say dup1 = numbersList1;, you're not copying the data structure, but copying the reference. This means that dup1 and numbersList1 are both pointing the same data structure, which means any change done to one of them will change the other (because they aren't really separate objects - it's more that you've just named the same thing both numbersList1 and dup1).

The reason for this is for performance. The majority of the time (passing objects to methods, getting data for temporary usage, etc.) you do not need to make a copy of the object, so it is faster to just grab the data that is already there. Imagine you had an array of 1000000 elements. Imagine having to copy that every time you used it as an argument to a method - a nightmare! It can be rather unintuitive to those unfamiliar with program$$anonymous$$g, but it does have its uses.

The work around, of course, is to explicitly say: "yes, I want to copy this now." You can do that manually, or there are methods to do it for you, but either way you have to explicitly ask for a copy, not just set one equal to the other.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Help With "For Loop" Not Working? 2 Answers

Using for as while. 3 Answers

Can't instantiate in loop - crashes unity 1 Answer

Using Loops 1 Answer

For loop going based off of Time.deltatime? 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