Having issues with implementing the pathfinding

Is it the same error, or a new one? I can’t see any more issues

Yeah, same issue, that the index is out of range.

In the testing script i even changed the starting location away from 0,0 to see if i would get different results, but the same thing happens.

Everything else works perfectly fine, it’s just when i hit the T to try to draw the line that the errors occur.

Yeah, sure, but is it the same index out of range. I pasted your code into my project, made the fix I suggested and get no error, but there are a lot of places where we work with indices and the error could just have shifted elsewhere, now

Let me double check. I had to move locations away from my programming computer. Getting unity installed on my laptop and i’ll check in a bit.

This is the message:

And when clicking on the error itself, it directs me to the testing script itself which looks identical to what is shown in the lecture changes.

I even copied and pasted the script from the changes into my code itself and still got the error.

The code is:

private void Update()

{

    if (Input.GetKeyDown(KeyCode.T))

    {

        GridPosition mouseGridPosition = LevelGrid.Instance.GetGridPosition(MouseWorld.GetPosition());

        GridPosition startGridPosition = new GridPosition(0, 0);

        List<GridPosition> gridPositionList = Pathfinding.Instance.FindPath(startGridPosition, mouseGridPosition);

        for (int i = 0; i < gridPositionList.Count - 1; i++)

        {

            Debug.DrawLine(

                LevelGrid.Instance.GetWorldPosition(gridPositionList[i]),

                LevelGrid.Instance.GetWorldPosition(gridPositionList[i + 1]),

                Color.white,

                10f

            );

        }

    }

}

OK, so that’s a different error entirely. The error says the problem is on line 23. You didn’t paste the fill script so I don’t know (and cannot accurately count) which line that is exactly. Add some debug logs in there and check if any of LevelGrid.Instance or Pathfinding.Instance is null. Also check gridPositionList

Line 23 begins at the for loop.
It seems it’s not even hitting the for loop at all. When i added basic debug logs in the loop, just to display the i, it didn’t run./

and it seems that when i try to run a log with the gridPositionList, it doesn’t want to display anything. So i’m betting thats where the issue is happening.

Good. So that suggests the Pathfinding.Instance.FindPath may not be creating or returning the list. What does the FindPath code look like? Is it creating the list? Does it return the list?

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;

}

I think the problem is here

This should return List<GridPosition>, not just List

i honestly dont know why it decided not to copy that part over, but that is what it’s written as in my script

oh, of course. Damn. It’s the html tags

Would you rather i just grab a series of images of the code so the tags done mess with anything else?

No, you can check this post on formatting code but I think it would be easier if you could zip the project and upload it somewhere so I can look at it all as a whole

Okay, i uploaded the zipped file to my google docs.

https://drive.google.com/file/d/1Bg-Vijk5Mlw9M_TO-QaafLoI7iUIwZHr/view?usp=sharing

All your PathNodes have the same gridPosition because you never set it.

Your PathNode constructor is empty. It should look like this

public PathNode(GridPosition gridPosition)
{
    this.gridPosition = gridPosition;
}
1 Like

Such a simple thing caused such a headache. Isn’t coding grand?

But it looks like everything is fully working now!

Thank you so much for your help!

1 Like

Indeed, it is. Sometimes the issue is several scripts away from the error message.

2 Likes

Yeah, we would have been going back-and-forth for a while if I couldn’t see the project. It wasn’t a straight-forward bug to find.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms