It looks like it’s hitting the end of the function and returning null. But from what i’m seeing, it looks like how it does in the course changes documentation. so maybe i’m missing something.
The function is:
public List FindPath(GridPosition startGridPosition, GridPosition endGridPosition)
{
List<PathNode> openList = new List<PathNode>();
List<PathNode> closedList = new List<PathNode>();
PathNode startNode = gridSystem.GetGridObject(startGridPosition);
PathNode endNode = gridSystem.GetGridObject(endGridPosition);
openList.Add(startNode);
//initializes the nodes
for (int x = 0; x < gridSystem.GetWidth(); x++)
{
for (int z = 0; z < gridSystem.GetHeight(); z++)
{
GridPosition gridPosition = new GridPosition(x, z);
PathNode pathNode = gridSystem.GetGridObject(gridPosition);
pathNode.SetGCost(int.MaxValue);
pathNode.SetHCost(0);
pathNode.CalculateFCost();
pathNode.ResetCameFromPathNode();
}
}
startNode.SetGCost(0);
startNode.SetHCost(CalculateDistance(startGridPosition, endGridPosition));
startNode.CalculateFCost();
while (openList.Count > 0) //cycling through the nodes
{
PathNode currentNode = GetLowestFCostPathNode(openList); //grabs the nodes based on the lowest Fcost
if (currentNode == endNode) //checks to see if the node is the final node
{
//reached the final node
return CalculatePath(endNode);
}
openList.Remove(currentNode); //removes the node from this list
closedList.Add(currentNode); // adds it to this list
foreach (PathNode neighbourNode in GetNeighbourList(currentNode)) //checks each neighbors of the node
{
if (closedList.Contains(neighbourNode)) // if the node is already on the closed list - was already searched and can ignore it
{
continue;
}
//caclulates a tenitive Gcost for the node based on the start node and the next node
int tentativeGCost =
currentNode.GetGCost() + CalculateDistance(currentNode.GetGridPosition(), neighbourNode.GetGridPosition());
if (tentativeGCost < neighbourNode.GetGCost()) // if it is smaller moving to the neighbor would be faster
{
neighbourNode.SetCameFromPathNode(currentNode);
neighbourNode.SetGCost(tentativeGCost);
neighbourNode.SetHCost(CalculateDistance(neighbourNode.GetGridPosition(), endGridPosition));
neighbourNode.CalculateFCost();
if (!openList.Contains(neighbourNode)) //if it's not yet added, add it to the open list
{
openList.Add(neighbourNode);
}
}
}
}
Debug.Log(“It hit null!”);
//no path found
return null;
}