2D Grid Graph not updating after an obstacle is destroyed

Hello all, I’m making a 2d top down game. The player can spawn lines that auto destroy themselves after a couple of seconds, they are obstacles for the enemy that follows the player. When I spawn an obstacle it works fine and the enemy recalculates the path correctly, but when the line is destroyed the nodes that were unwalkable because of the line are still unwalkable except the nodes on the middle of the line that are walkable again, even if I made an update of the graph just before the line dies. The line has 2 colliders 1 is for the player and the enemies the other is in a child object of the line and is in another layer (the layer is “nodes”) the grid checks the collisions only in the nodes layer. Here’s the code:

    // Update is called once per frame
    void Update()
    {
        //if time's off destroy the line
        if (time_duration_line <= 0 && !Input.GetKeyDown(KeyCode.Space))
        {
            ...
            //update the graph
            //get the child's collider (that's the collider that handles 
            //the collisions with the nodes 
            Collider2D child_node_colz = gameObject.GetComponentInChildren<BoxCollider2D>();
            //disable the collider that so the bounds will be "empty"
            child_node_colz.enabled = false;
            AstarPath.active.UpdateGraphs(child_node_colz.bounds);
            Destroy(gameObject);

        }
}

What should I change?
Thank you in advance guys :smiley:

Hi

I am not sure why you do this before you update the graph. If you comment is correct and the bounds are empty (I don’t work with the 2D colliders that often, so I don’t remember their behavior) then you will try to update the graph using an empty bounds, which is probably not what you want.
Try to capture the bounds field before you disable the collider.

I did as you said and it worked! I also modified the code, I made the update in a different script

public class UpdateGraphs : MonoBehaviour {

    BoxCollider2D colz;
    GraphUpdateObject graph_update_obj;
    LineCheckPolygon line_check_poly;
    
	// Use this for initialization
	void Start () {
        colz = gameObject.GetComponent<BoxCollider2D>();
        line_check_poly = gameObject.GetComponentInParent<LineCheckPolygon>();
        AstarPath.active.UpdateGraphs(colz.bounds);
    }

    // Update is called once per frame
    void Update () {
        //if it can update the nodes before destroying the obstacle
        if(line_check_poly.delete_colz_nodes)
        {
            graph_update_obj = new GraphUpdateObject(colz.bounds);
            graph_update_obj.updatePhysics = true;
            //updates the graph
            AstarPath.active.UpdateGraphs(graph_update_obj);
            //disables the collider
            colz.enabled = false;
            Destroy(transform.parent.gameObject);
        }
    }
}

Why if I disable the collider before the update it doesn’t work? Isn’t it the same thing?

Thank you very much for your great help aron! :smile:

Hi

If you capture the bounds after you have disabled the collider the bounds will not be correct (they will be empty), so you need to disable the collider after you capture the bounds.