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