Having issues with implementing the pathfinding

I followed along with the lecture, but every time I press ‘T’ to draw the line for the path, it doesn’t appear, the console says that i’m getting an IndexOutOfRangeException

To be more specific

IndexOutOfRangeException: Index was outside the bounds of the array.
GridSystem`1[TGridObject].GetGridObject (GridPosition gridPosition) (at Assets/Scripts/Grid/GridSystem.cs:67)
Pathfinding.GetNode (System.Int32 x, System.Int32 z) (at Assets/Scripts/Pathfinding.cs:136)
Pathfinding.GetNeighbourList (PathNode currentNode) (at Assets/Scripts/Pathfinding.cs:149)
Pathfinding.FindPath (GridPosition startGridPosition, GridPosition endGridPosition) (at Assets/Scripts/Pathfinding.cs:81)
Testing.Update () (at Assets/Scripts/Testing.cs:21)

I looked through the project changes to make sure that everything matched up to the lecture, and i’m not seeing anything different, but i keep getting the error and can’t get the line to appear

It’s hard to say without seeing the code. Can you show the code for PathFinding.GetNeighbourList?

Sure, this is the code

private List GetNeighbourList(PathNode currentNode)

{

    List<PathNode> neighbourList = new List<PathNode>();

    GridPosition gridPosition = currentNode.GetGridPosition();

    if (gridPosition.x - 1 < 0)

    {

        // Left node

        neighbourList.Add(GetNode(gridPosition.x - 1, gridPosition.z + 0));

        if (gridPosition.z - 1 >= 0)

        {

            // Left Down node

            neighbourList.Add(GetNode(gridPosition.x - 1, gridPosition.z - 1));

        }

        if (gridPosition.z + 1 < gridSystem.GetHeight())

        {

            // Left Up node

            neighbourList.Add(GetNode(gridPosition.x - 1, gridPosition.z + 1));

        }

    }

    if (gridPosition.x + 1 < gridSystem.GetWidth())

    {

        // Right node

        neighbourList.Add(GetNode(gridPosition.x + 1, gridPosition.z + 0));

        if (gridPosition.z - 1 >= 0)

        {

            // Right Down node

            neighbourList.Add(GetNode(gridPosition.x + 1, gridPosition.z - 1));

        }

        if (gridPosition.z + 1 < gridSystem.GetHeight())

        {

            // Right Up node

            neighbourList.Add(GetNode(gridPosition.x + 1, gridPosition.z + 1));

        }

    }

    if (gridPosition.z - 1 >= 0)

    {

        //Down

        neighbourList.Add(GetNode(gridPosition.x + 0, gridPosition.z - 1));

    }

    if (gridPosition.z + 1 < gridSystem.GetHeight())

    {

        //Up node

        neighbourList.Add(GetNode(gridPosition.x + 0, gridPosition.z + 1));

    }

    return neighbourList;

}

You are only checking invalid positions here

This should be

if (gridPosition.x - 1 >= 0)
2 Likes

It looks like i’m still getting the error message even after making the change.

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

Privacy & Terms