For some reason I’m getting a StackOverflow. I’m keeping track of all the visited and making sure I don’t revisit those nodes, but nevertheless, I’m still getting the issue.

Here’s the code:

```
#region Finding all generators connected to this machine
public void GetAllConnectedGenerators()
{
if (direction == null)
return;
HashSet<(int, int)> visited = new HashSet<(int, int)>();
generators.Clear();
// GET THE STARTING X AND Y COORDINATE OF THIS MACHINE
int x = direction.gridLocation.Item1;
int y = direction.gridLocation.Item2;
// IF THE OBJECT BELOW THE MACHINE IS NOT A CABLE THEN RETURN, NO NEED TO CHECK ANY OTHERS BECAUSE IT HAS NO CONNECTIONS
if (!IsCable(x, y))
return;
// ADD THIS CABLE TO THE VISITED HASHSET
visited.Add((x, y));
// START THE LOOP
GetSurroundingCables(x, y, visited);
// NEED TO ALSO CHECK THIS TILE FOR A GENERATOR, I THINK THIS IS IMPOSSIBLE BUT WE DON'T WANT TO RULE IT OUT
GetConnectedGenerator(x, y);
}
public void GetSurroundingCables(int x, int y, HashSet<(int, int)> visited)
{
// NORTH
// CHECK IF THE GAMEOBJECT AT THE LOCATION IS A CABLE
if (!visited.Contains((x, y + 1)))
{
if (IsCable(x, y + 1))
{
// GET ANY GENERATORS CONNECTED TO THAT CABLE
GetConnectedGenerator(x, y + 1);
// LOOP OVER THE NEXT CABLE
GetSurroundingCables(x, y + 1, visited);
// ADD THIS AS A VISITED TILE
visited.Add((x, y + 1));
}
}
// EAST
if (!visited.Contains((x + 1, y)))
{
if (IsCable(x + 1, y))
{
GetConnectedGenerator(x + 1, y);
GetSurroundingCables(x + 1, y, visited);
visited.Add((x + 1, y));
}
}
// SOUTH
if (!visited.Contains((x, y - 1)))
{
if (IsCable(x, y - 1))
{
GetConnectedGenerator(x, y - 1);
GetSurroundingCables(x, y - 1, visited);
visited.Add((x, y - 1));
}
}
// WEST
if (!visited.Contains((x - 1, y)))
{
if (IsCable(x - 1, y))
{
GetConnectedGenerator(x - 1, y);
GetSurroundingCables(x - 1, y, visited);
visited.Add((x - 1, y));
}
}
}
public bool IsCable(int x, int y)
{
// IF THE ELECTRIC GRID DOES NOT CONTAIN AN OBJECT AT THAT LOCATION, THEN RETURN FALSE
if (grid.electricGrid[x, y] == null)
return false;
// IF THE GAMEOBJECT AT THE LOCATION HAS THE TAG "CABLE" THEN RETURN TRUE
if (grid.electricGrid[x, y].tag.Equals("Cable"))
return true;
return false;
}
public void GetConnectedGenerator(int x, int y)
{
// IF THE GAMEOBJECT AT THAT LOCATION IS NULL THEN RETURN
if (grid.grid[x, y] == null)
return;
// GET THE CONNECTOR COMPONENT OF THAT GRID LOCATION
Connector conn = grid.grid[x, y].GetComponent<Connector>();
// IF CONN IS NOT NULL
if (conn != null)
{
// IF THE GENERATORS DOES NOT CONTAIN THIS GENERATOR
if (!generators.Contains(conn))
// ADD THE GENERATOR
generators.Add(conn);
}
}
#endregion
```

What could be causing this?