- Home /
Why do I have multiple nav meshes in my scene?
I am generating a city on a grid with x * y amount of tiles. There are 14 different building prefabs, an street in z direction, one street in x direction and a crossroad for when two streets cross.
In the inspector I can set x and y values of how large the grid should be, a gameobject with the city generation script and in that scrip I have a reference to an unbaked navmesh object that I have in the scene.
Once I start the script will first generate a city grid with random buildings, then in the end I run navMeshSurface.BuildNavMesh().
What happens is that the original navmesh is built and is shaped just as I want it however, there are also as many clones of that nav mesh as there are buildings in the scene which seems strange. Why is that?
  void Start()
     {
         mapGrid = new int[mapWidth, mapHeight];
         SortBuildingsBySize(); //Sorts the building array to have the shortest building first in case I put new buildings into it
 
         float seed = UnityEngine.Random.Range(0, 100);
         GenerateMapData(seed);
     }
     
 //Generates a city grid with perlin noise
     private void GenerateMapData(float aSeed)
     {
         float buildingAmount = (float)buildings.Length;
         for (int h = 0; h < mapHeight; h++)
         {
             for (int w = 0; w < mapWidth; w++)
             {
                 mapGrid[w, h] = (int)(Mathf.PerlinNoise(
                 w / buildingAmount + aSeed,
                 h / buildingAmount + aSeed
                 ) * buildingAmount);
             }
         }
 
         BuildStreetsIntoMapData(); //Randomly places streets along x and z axis in the grid
 
         GenerateCity();
     }
 
     private void GenerateCity()
     {
         for (int h = 0; h < mapHeight; h++)
         {
             for (int w = 0; w < mapWidth; w++)
             {
                 int result = mapGrid[w, h];
                 Vector3 pos = new Vector3(w * buildingFootprint, 0, h * buildingFootprint);
 
                 if(result < -2)
                 {
                     createdBuildings.Add(Instantiate(crossRoad, pos, crossRoad.transform.rotation));
                 }
                 else if(result < -1)
                 {
                     createdBuildings.Add(Instantiate(xStreet, pos, xStreet.transform.rotation));
                 }
                 else if (result < 0)
                 {
                     createdBuildings.Add(Instantiate(zStreet, pos, zStreet.transform.rotation));
                 }
                 else
                 {
                     GameObject building = null;
                     for (int buildingIndex = 0; buildingIndex < buildings.Length; buildingIndex++)
                     {
                         if (result < buildingIndex)
                         {
                             building = buildings[buildingIndex];
                             pos.x = w * buildingFootprint;
                             pos.y = building.transform.position.y;
                             pos.z = h * buildingFootprint;
                             buildingIndex = 10000;
                         }
                     }
 
                     if (building != null)
                     {
                         createdBuildings.Add(Instantiate(building, pos, Quaternion.identity));
                     }
                 }
             }
         }
 
         foreach (var building in createdBuildings)
         {
             building.transform.parent = transform;
         }
 
         navMeshSurface.BuildNavMesh(); //Bakes navmesh once all buildings ar in the scene.
     }
is posible the nav$$anonymous$$eshSurface.BuildNav$$anonymous$$esh() is inside a for ? so its executed multiple times? 
No, it is definitely outside of any loop. I set up a debug log next to the BuildNavmesh call and only get one message.
$$anonymous$$ake sure there is no ahy other thing creating a navmesh. Are you sure you have 0 nav mesh before that line in executed and multiple navmeshes after that line is executed?
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Click to Move - get NavMesh Area Name 2 Answers
Mesh generated from bezier curve loop going outside loop 0 Answers
Unity pathfinding - Comparing 2 paths? 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                