SpawnOnBake (Sample Script)

Provided as a part of the Samples

Spawn On Bake (Sample)

Namespace: MegaCrush.NavmeshBaker.Sample

Purpose

Safely spawn characters or AI agents after the first successful runtime bake, avoiding errors like:

“Failed to create agent because there is no valid NavMesh.”


How it works

  • Subscribes to BakerEvents.OnBakeCompleted.

  • On each completed bake:

    • Chooses a spawn position (from Spawn Point or the GameObject this script is attached to).

    • Calls NavMesh.SamplePosition() to snap to a valid NavMesh point.

    • Instantiates the prefab.

  • If Only Once is enabled, ignores subsequent bakes.


Inspector

  • Prefab (GameObject) The object to spawn (e.g., enemy, NPC, player). If it’s an AI, it should include a NavMeshAgent.

  • Spawn Point (Transform, optional) If set, the prefab spawns here; otherwise it spawns at this component’s transform.

  • Only Once (bool) If enabled, spawns on the first completed bake only.

  • Sample Max Distance (float) Radius around the desired position to search for a valid NavMesh point.

  • Area Mask (int) NavMesh area mask for sampling (default: NavMesh.AllAreas).


Example usage

  1. Ensure your scene has a BakerCoordinator with a NavMeshBakeProfile and Center Target.

  2. Add one or more DynamicNavMeshSurface components for the regions you want baked.

  3. Create an empty GameObject and add SpawnOnBake.

  4. Assign a Prefab (e.g., an enemy with NavMeshAgent).

  5. Optionally assign a Spawn Point.

  6. Press Play — once the first bake finishes, the prefab spawns on valid NavMesh.


Why it’s useful

  • Guarantees AI never spawns off-navmesh.

  • Robust for procedural or streaming worlds where navmesh is generated at runtime.

  • Demonstrates listening to BakerEvents.OnBakeCompleted to gate gameplay logic.


Script

Last updated