i have this snippet of code that is used to retrace the path found by the a* algorithm. for some reason i can't wrap my head around, it throws a nullreferenceexception:
void drawpath(GridLocation start, GridLocation end) {
List<GridLocation> path = new List<GridLocation>();
GridLocation current;
current = end;
while (current != start) {
path.Add(current);
current = current.parent;
}
Console.WriteLine(path.Count);
path.Reverse();
grid.path = path;
}
the error i'm getting is System.NullReferenceException: 'Object reference not set to an instance of an object.'current was null.
following is the implementation for my GridLocation class:
public class GridLocation {
public bool isPassable;
public int gcost, hcost;
public int value, x, y;
public GridLocation parent;
public GridLocation(bool _isPassable, int _x, int _y, int _value) {
isPassable = _isPassable; x = _x; y = _y; value = _value;
}
//gets the total cost of the nodes based on the hcost and gcost variables
public int fcost {
get {
return (gcost + hcost);
}
}
}
and the way i call the method:
if (current.value == target.value) {
drawpath(start, target);
Console.WriteLine("Path found!!!");
return;
}
worth noting is that if i try checking if the objects are the same (like this if (current == target)
) nothing happens
edit: i'm new to c#, and i already tried everything else, that's why i'm posting here.