Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JNetRocks1316 · Mar 09, 2014 at 01:40 PM · aispawnnavmeshnavmeshagent

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
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image dyankov · Jun 11, 2014 at 04:41 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NavMesh Agent Obstacle Avoidance Ignore 1 Answer

How to make AICharacterController able to walk on walls and ceilings? 0 Answers

What’s the simplest way to find nearest navmesh targets? 0 Answers

Move NavMeshAgent at a constant speed 1 Answer

Keep AI from flipping? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges