- Home /
how to use two foreach loops into another correctly
i have two foreach loops into another. just for correct understanding, if would have in both foreach loop two objects that are looped through, so both of the first two would check with the both of the loop inside, so there would be four check? but if i use a code like this, the first two objects will check only with the last of the two inside gameobjects. cn someone tell me what i'm thingking wrong and if there is a correct way to use two foreach loops? thank you!
foreach (GameObject go in gos)
{
foreach (GameObject otherGo in otherGos)
{
// check something
}
}
Answer by Hoeloe · Oct 01, 2013 at 09:52 AM
Fattie is right, you should make sure to mark correct answers as such, so that the question can be closed.
Aside from that, your issue is with what are known as "nested loops". Loops work like this: All the code inside the {} enclosing the loop will be executed for each element in the loop. This includes other loops. If, for example, your arrays gos
and otherGos
each have 2 elements, and let's assume each holds 2 integers, so gos = {1, 2}
and otherGos = {3, 4}
, and you run this code:
foreach (int go in gos)
{
foreach (int otherGo in otherGos)
{
Debug.Log(go + " : " + otherGo);
}
}
(This is virtually identical to the code you've written up there. The only difference is the types have been swapped, and a log statement added in to demonstrate), you will get the following output:
1 : 3
1 : 4
2 : 3
2 : 4
Why is this? Well, the code runs through each member of gos
in turn, so it runs all the code for the first value (1), then repeats it all for the second value (2). So, in the first iteration of the loop, it runs:
foreach (int otherGo in otherGos)
{
Debug.Log(go + " : " + otherGo);
}
with go
set to 1. When it hits the other loop, however, it runs through all the elements of otherGos
in turn, too, but the key point is that it doesn't exit the previous loop, so while it's doing this, the first loop is still in its first iteration (that is, with go
set to 1). When this inner loop has finished (this first iteration of the outer loop accounts for the first 2 print statements), only then does it return to the outer loop and look at the next element, setting go
to 2, and then repeating the inner loop with go
at 2. This accounts for the next 2 print statements. You could extend this further. If you ran exactly the same code with gos = {1, 2, 3, 4} and otherGos = {5, 6, 7}, you would get:
1 : 5
1 : 6
1 : 7
2 : 5
2 : 6
2 : 7
3 : 5
3 : 6
3 : 7
4 : 5
4 : 6
4 : 7
I'm sorry. I went through all my questions and thicked them! Thank you very much for your detailed answer! That is exactly how I imagined the two foreach loops to work and also how I used them. But the problem is, it will only return me the value of one bool (if I log it) this way. But it actually should return me the four different combiantions of the bools, right? For example something like true : true, true : false, false : true, false : false or so. Why doesn't this work?
foreach (int go in gos)
{
if (someCondition)
{
someBool = true;
}
else
{
someBool = false;
}
foreach (int otherGo in otherGos)
{
if (someOtherCondition)
{
someOtherBool = true;
}
else
{
someOtherBool = false;
}
}
}
Please, when asking a question, don't EVER ask "why doesn't it work?". That is the vaguest, most unhelpful thing you could ask. Tell us what it is doing, in comparison to what you expect it to do. When you say it doesn't work, HOW doesn't it work? Does it give you errors? Does it always give you "true"? Does it not give you anything?
yeah right, sorry. i just thought it would be a basic mistake a beginner makes anyway and it is for you actually obvious why this with the bools can not work. that's why i was asking the 'why doesn't it work' question. it doesn't give any errors. the thing is that if i log it just one bool is shown. if i press play, the scripted reaction to the bool works for the two 'go' in 'gos' and for the last 'otherGo' in 'otherGos', but none of the 'go' will check with the first 'otherGo' in the array. if i log as you said (go + " : " + otherGo); in the secound foreach loop, it will show all four possibilities. if i log (someBool + " : " + someOtherBool) it, as i said, just gives me back one single bool combination. i tried to explain myself that this is because bools can just store one information. if i check these foreach loops with floats ins$$anonymous$$d of bools, it will log the four combinations again. so i solved the problem with storing the needed information by changing float values and then ask for them ins$$anonymous$$d of checking if one of the bools is true or false. thanks for the answers!
Hi there @wolga, can you please TIC$$anonymous$$ an answer, to close out the question. Only you can do it, as the questioner. Thanks. It's the round tick symbol.
I'd like to see the code you tried for your attempt with booleans, because it should have worked perfectly. Also, as a general rule, avoid floats if you possibly can, they are prone to horrible inaccuracy.
Your answer
Follow this Question
Related Questions
Using ForEach Loop to change variables on prefabs 1 Answer
Raycasting and foreach loops 1 Answer
"not all code paths return a value" i really try it and i really don't have idea 3 Answers
How to have a loop run itself again if returned value is a known exception, with a new return value. 1 Answer
Not able to make a foreach loop 2 Answers