Short question about for each loop and gameObjects.

Hello Guys,

I would like to get help in some shorts question about foreach loop.

In the code below, there is a command that say
if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) {
the command refer to a single and a certain “neighbour” from the list or any of them ?

Thank you in advance.

foreach (Node neighbour in grid.GetNeighbours(currentNode))
            {
                if (!neighbour.walkable || closeSet.Contains(neighbour))
                {
                    continue;
                }
                int newMovementCostToNeighbour = currentNode.gCost + GetDistance(currentNode, neighbour);

          
                if (newMovementCostToNeighbour < neighbour.gCost || !openSet.Contains(neighbour)) {
                    neighbour.gCost = newMovementCostToNeighbour;
                    neighbour.hCost = GetDistance(neighbour, targetNode);
                    neighbour.parent = currentNode;

                    if (!openSet.Contains(neighbour))
                        openSet.Add(neighbour);
                }
            }

The foreach construct implies it is only a single Node. It also implies that grid.GetNeighbours() returns some kind of IEnumerator that foreach traverses, one Node item at a time.

Thank you very much for your answer,
Is it true that is iteration of the foreach will call all what inside {} of the foreach loop right, and will ofcourse will refer a single and certain “neighbour”

I understand it correctly?

Yes indeed!

Thank you again you saved me!