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
1
Question by WakingDragon · Jun 22, 2021 at 09:14 AM · spawnnavmeshnavmeshagentpooling

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?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

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

128 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 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 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 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 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 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 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

How does NavMeshAgent.SamplePathPosition work? 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