Cannot update the grid graph at runtime

Hello,
I’m trying to update my grid everytime I add a specific new gameobject I instantiate.
However this does not seem to work.
I even tried to loop over all cities after they have been spawned and update the graph but this does not work as well. What am I doing wrong?

    private void SpawnCity(CityScriptableObject cityObject, Database database)
    {
        GameObject cityGameobject = Instantiate(database.GetCityPrefab());
        cityGameobject.name = cityObject.CityNameLocalizedKey + "-" + cityObject.Id;
        cityGameobject.transform.parent = _cityContainer;

        // Calculate city terrain height
        float terrainHeightPosition = Terrain.activeTerrain.SampleHeight(cityObject.CityPosition) + 0.5f; // TODO: Remove this fixed height and in the end don't even calculate it via this. The map editor wil position is correctly.
        Vector3 cityCalcPosition = new Vector3(cityObject.CityPosition.x, terrainHeightPosition, cityObject.CityPosition.z);

        cityGameobject.transform.position = cityCalcPosition;
        cityGameobject.transform.rotation = cityObject.CityRotation;

        Bounds bounds = cityGameobject.GetComponent<Collider>().bounds;
        GraphUpdateObject guo = new GraphUpdateObject(bounds);
        AstarPath.active.UpdateGraphs(guo);
    }

Hi

If you click the Scan button in the inspector manually, does the graph look like you want it to?

Yeah, doing it manually works but not via code.

Can you verify that the bounding box that you get actually covers the whole city?

Yeah, it definitely covers it. Thats why it works when scanning it manually.
I’m really clueless why it’s not working.

I mean, it’s not the case that there are colliders on any children GameObjects that extend further out? it is the root GameObject’s collider that covers everything?

Hmm. Also. I’m not quite sure if this is the case, but the collider bounds may not be accurate after moving the transform. Try to call Physics.SyncTransforms() right before you get the bounds.

1 Like

Wow that was really the case, it fixed that!
Thank you so much.