- Home /
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.
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++) {
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)
You say you operate on copies, but are you really? (cough line26 cough)
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613115233im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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