- Home /
Good method for handling spawning of navmesh agents when get "Failed to create agent" error
Here is the question that I have been trying to answer for ages through searches online. I have written an answer to it. If it gets a few upvotes then I will mark it as SOLVED for other users.
I am using an object pool to spawn units (although this would apply as much to instantiating the units) at runtime. Their spawn points are worked out dynamically. They are navmesh agents using Unity's inbuilt (and awesome) AI system. When units are destroyed, they are deactivated using SetActive(false) and returned to the pool for re-use.
I kept getting "Failed to create agent because it is not close enough to the NavMesh" errors. How to solve this problem and get properly instantiated units?
Answer by WakingDragon · Jun 22, 2021 at 09:48 AM
And here is the answer I have found. This is not a "pro" answer, but it has worked for me.
The problem: NaveMeshAgent freaks out if it is in place but there is no navmesh beneath it. Even for 1 frame. To prevent these errors, I needed to carefully manage the NavMeshAgent component.
Here's how it worked for me: 1. Create a simple UnitState enum (packed,active) 2. Create a UnitDeployer script that receives messages to spawn or repool/destroy the Unit, and that tells other components on the Unit that they should enter active or packed state. 3. Put timer coroutines on the UnitDeployer a) when asked to destroy/repool the Unit, it sends the message to enter packed state then waits 0.1f before destroy/repool. b) when creating/getting from pool, the timer waits 0.1f before telling components to enter active state. 4. Create a UnitNavMeshAgentController script. Here is that script in pseudo-code...
bool m_iOnNavMesh
NavMeshAgent m_agent
UnitState m_state
private void Update
{
if (m_state == UnitState.active && !m_isOnNavMesh)
{
m_isOnNavMesh = NavMesh.SamplePosition (returns a bool) //checks agent is on mesh
if(m_isOnNavMesh && m_agent == null)
{
SetupAgent() //only setup agent if there is not one already and we are on the mesh
}
}
private void SetupAgent()
{
//add navmeshagent component and set it up with speed etc
}
public void SetToActiveState() //called by UnitDeployer
{
m_state = UnitState.active //allows the m_isOnMesh check to happen
}
public void SetToPackedState() //called by UnitDeployer
{
Destroy(m_agent) //destroy component to stop warnings when agent moved off navmesh
m_state = UnitState.packed
}
}
This is just pseudo-code but I have found, as a beginner, that this sort of "architectural" help is really useful. NavMesh is so beautifully simple to use but this is one quirk that really bugs me. I'd love it if NavMeshAgent had a simple m_agent.ConnectToMesh()/.DisconnectFromMesh() function!
Your answer
Follow this Question
Related Questions
Navmesh After Spawn 2 Answers
Pooled objects change behavior. 0 Answers
Navmesh problem with SpawnSystem 0 Answers
Instantiating Enemy AI NavMesh Agent SetDestination Error 1 Answer