We use NavMeshAgents extensively, and in one of our scenes we get this pair of errors when a select group of NavMeshAgents gets enabled
!InCrowdSystem
!handle.IsValid()
The objects are instantiated in runtime, and placed at certain points on the NavMesh at intervals.
When looking into this I also noted that all the objects got the warning
“Failed to create agent because it is not close enough to the NavMesh”, due to them being temporarily instantiated in thin air, and then teleported to their appropriate place. I got rid of this warning by letting the NavMeshAgent component on the prefab start out disabled, and getting enabled first after they had been placed. This solved the warning, but the above errors remain.
The errors pop up when the NavMesh agent first gets enabled after being instantiated. The object in question is used throughout the game and the NavMeshAgent handled the same way, but I’ve only seen the error in the case of these objects that are instantiated in runtime. The others are pre-placed in the scene.
The errors do not show up if I just toggle the component on/off, or deactivate/activate the game object. That works fine. But when the object dies, the object is deactivated, it is teleported and re-activated somewhere else, and the error pops up again when the NavMeshAgent is re-enabled.
I haven’t noticed any off about the NavMeshAgent, it seems to e behaving as it should once it’s doing its navmesh thing. I just want to know the cause and how to do whatever we’re doing wrong correctly.
I’m also seeing these errors. My use case sounds similar except the NavMeshAgent already exists in the scene. Here’s what I’m seeing:
The NavMeshAgents already exist in the scene and are on their NavMeshes.
When the game starts up, a chunk of the scene (that the above agents are children of) gets disabled due to a culling system I have for different regions.
When the player enters the region, the above chunk gets enabled again. This is where I get those 2 errors (one set for each agent).
It’s definitely related to setting nav objects active after being inactive. But it only happens once, which makes me think it’s internal to the initialization of the NavMeshAgent.
The order may matter, I don’t know. Like you, I’d guess that the problem has to do with how the states and velocities are updated and tracked internally. Perhaps the big jump causes a discontinuity internally that it catches and uses to throw the exception.
Other things to try:
On respawn but before reactivating the nav agent, set the destination to the new position (don’t use SetDestination, just use the .destination property.
Try manually setting the velocity to zero before and after moving the nav agent, with the agent.velocity property.
Instead of manually moving the nav agent, use the agent.Warp() function call - since its designed to instantaneously move the agent on the nav mesh, it may avoid internal errors.
Possibly try issuing the agent.Stop() command before moving the agent.
We have the same error under similar conditions: does it duplicate navmesh agents as phantoms?
I started investigating this error since we also get undead navmesh agents without gameobject references in the scene, which crowd the invisible space.
Basically they survive game restarts. Might be completely unrelated, but it is weird having this invisible crowd of non-people rush in when you open the door after a long development session…