title: Unity: How to find all neighbors recursively (neighbors of neighbors)?
body: <p>I'm making a Bubble Shooter game in Unity. I came to the point where I can hit another bubble and it destroys all of its neighbors.</p>
  
  <p>Now I'm trying to destroy all of its neighbors' neighbors and it causes a stack overflow. I was using a recursion.</p>
  
  <p>I did it without the recursion, and found its second-tier neighbors just to see if the logic works out. It does. The problem is with the way I'm using a recursion.</p>
  
  <pre><code>    private List&lt;Bubble&gt; FindAllRecursiveNeighbors(Vector2Int originPosition)
  {
      List&lt;Bubble&gt; allNeighbors = FindNeighbors(originPosition);
  
      List&lt;Bubble&gt; result = new List&lt;Bubble&gt;();
  
      foreach (Bubble bubble in allNeighbors)
      {
          if (result.Contains(bubble)) { continue; }
          result.Add(bubble);
      }
  
      // Recursion starts here.
      foreach (Bubble bubble in result)
      {
          List&lt;Bubble&gt; neighbors = FindAllRecursiveNeighbors(FindPositionOfBubble(bubble));
          foreach (Bubble neighbor in neighbors)
          {
              if (result.Contains(neighbor)) { continue; }
              result.Add(neighbor);
          }
      }
  
      return result;
  }
  </code></pre>
  
  <p>I expected all the bubbles in a line to be destroyed. I got the stack overflow error. If I delete the recursive part it works, but just for the immediate neighbors.</p>
  
  <p>The error is this: StackOverflowException: The requested operation caused a stack overflow, and it is in the line where I'm calling the FindAllRecursiveNeighbors again.</p>