- Home /
Navmesh error on instantiated navmesh agents
Hi. So I'm using navmesh for my enemies AI, and it works fine when I manually place an enemy down. But once I start spawning them in using Instantiate I get an error saying:
"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
Even though the enemies are clearly on the nav mesh. Here's a picture:
As you can see the Agent is on a navmesh.
Here's my code for isntantiating them:
// Determine a random position inside the spawner to spawn
float xPos = transform.position.x + Random.Range(-spawner.radius, spawner.radius);
float zPos = transform.position.z + Random.Range(-spawner.radius, spawner.radius);
Vector3 spawnPos = new Vector3(xPos, transform.position.y, zPos);
// Instantiate it
GameObject tempMonster = (GameObject)Instantiate(monster, spawnPos, Quaternion.identity);
// Finding it's feet and placing it on the ground if it's floating
SkinnedMeshRenderer ren = tempMonster.GetComponentInChildren<SkinnedMeshRenderer>();
Ray ray = new Ray(ren.bounds.min, Vector3.down);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
tempMonster.transform.Translate(new Vector3(0, -hit.distance, 0));
// This is the AI script that calls NavMeshAgent.destination
EnemyAI eAI = tempMonster.GetComponent<EnemyAI>();
So it get's instantiated and then if it's floating it gets placed on the ground. Then the EnemyAI script is added which calls NavMeshAgent.destination = aposition;
I don't understand why there's an error with my spawned in ones and not with the ones I manually place in the scene. Can anyone help?
I'm having similar problems right now, ever find out what was the problem?