private GridElement setGoal(GridElement goal) { if (goal.isPassable() == null) { var possibleGoals = getNeighbours(goal); foreach (var x in possibleGoals) { if (x.isPassable() != null) { return(x); } } if (possibleGoals.Count == 0) { return(goal); } int index = Random.Range(0, possibleGoals.Count); return(setGoal(possibleGoals[index])); } return(goal); }