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);
}