- Home /
Instantiating Enemy AI NavMesh Agent SetDestination Error
Short Version:
I'm getting this error: "SetDestination" can only be called on an active agent that has been placed on a NavMesh. EnemyAI:NewDestination(Vector3)(at../EnemyAI.cs:36)
I do have a baked NavMesh
The Method is working fine on the enemies that are FIRST instantiated by the spawner
After the enemy dies and the Respawn method is invoked I SOMETIMES get this error
Maybe about 10% of the time
When this error pops up it spawns DOUBLE the number of enemies
So I'll have 1, then 2, then 4, then 8, etc.
It is only supposed to spawn 1 and then respawn it on death
Details:
I'm working on a respawning enemy and the way I went about it was to create a SpawnPoint prefab I can place into the world. It has a public gameObject variable called enemyType which I can assign any enemy prefab into.
EnemySpawn (Attached to the spawner gameobject):
public class EnemySpawn : MonoBehaviour {
public GameObject enemyType;
void Start () {
Spawn(0);
}
//Get ready to spawn the enemy
public void Spawn(int respawnTime){
Invoke("Respawn", respawnTime);
}
//Instantiate the enemy and set a reference to this spawnPoint's script
public void Respawn(){
GameObject go = Instantiate(enemyType, transform.position, transform.rotation) as GameObject;
EnemyHealth eh = go.GetComponent ("EnemyHealth") as EnemyHealth;
eh.spawnPoint = this;
}
}
The Enemy is spawned and it has two scripts: EnemyHealth and EnemyAI. EnemyHealth monitors it's health, unsurprisingly. :P When the health is at the appropriate level it's Die() method is called:
EnemyHealth: Attached to each enemy instance
public class EnemyHealth : MonoBehaviour {
public int targetsLeft = 0;
public int cd_respawn = 0;
//spawnPoint is set when instantiated by the spawner gameObject
public EnemySpawn spawnPoint;
void Die(){
//As long as iti's respawnable and has a respawn point, respawn the enemy
if(cd_respawn > 0 && spawnPoint != null){
spawnPoint.Spawn (cd_respawn);
}
//Turn off the enemyAI so it stops the InvokeRepeating wandering stuff
EnemyAI ai = this.GetComponent("EnemyAI") as EnemyAI;
ai.Death ();
//Destroy the current enemy
Destroy(gameObject);
}
}
The Enemy AI by default goes into Wandering state where it picks random locations using an InvokeRepeating method "Wander". Wander does some random math then calls NewDestination which uses agent.SetDestination with that randomized number. I Split these into two methods so I could manually use NewDestination if I wanted to.
Enemy AI: Attached to each instance of the enemy
public class EnemyAI : MonoBehaviour {
public NavMeshAgent agent;
private Vector3 startPosition;
//Variables ...
//...
void Awake(){
//Get the NavMeshAgent so we can send it directions and set start position to the initial location
agent = GetComponent("NavMeshAgent") as NavMeshAgent;
agent.speed = wanderSpeed;
startPosition = this.transform.position;
InvokeRepeating("Wander", 1f, 5f);
}
void Wander(){
//Pick a random location within wander-range of the start position and send the agent there
Vector3 destination = startPosition + new Vector3(Random.Range (-wanderRange, wanderRange),
0,
Random.Range (-wanderRange, wanderRange));
NewDestination(destination);
}
public void NewDestination(Vector3 targetPoint){
//ERROR LOCATION ******* ERROR LOCATION ******* ERROR LOCATION ******** ERROR LOCATION
//Sets the agents new target destination to targetPoint parameter
agent.SetDestination (targetPoint);
}
//Other Enemy AI functions not related to setDestination here
//...
//...
void StopChasing(){
chasing = false;
//Return Home then start wandering
agent.speed = wanderSpeed;
agent.SetDestination (startPosition);
InvokeRepeating("Wander", 0.5f, 5f);
}
//I added this method thinking the issue was that Wander might still be invoking
//It's called from EnemyHealth (Die) to make sure this AI stops before being deleted
//To be sure that wasn't throwing the error.
public void Death(){
CancelInvoke();
}
} //Class End
I'm having the exact same issue. I have enemies which are respawning after they die, but I'm getting this error whenever I move them to their spawn position. I tried using Nav$$anonymous$$eshAgent.Warp() - same result. Have you found a solution?
Answer by Bunnybomb7670 · Jun 11, 2014 at 04:44 PM
This is a big issue I ran into with my 2D game. Its a really trivial issue, sometimes you can fix it, sometimes you cant, firstly try rebuilding your nav meshes, move them around, try that, if that does not work, a possible alternative and " hacky " method is to enable and disable the nav-mesh component within the scripts start / awake function ( sometimes works for me ) and if that does not work, try make a new scene and try it there.
The issue may also be caused by the destination being slightly too far from the ground to be noticed as a possible place to reach.