- Home /
Enable/Disable the NavmeshAgent programatically?
Is there a way to uncheck the Navmesh Agent check box programatically? Can it be done using a NavMeshAgent object?
Change to:
Answer by Mmmpies · Dec 15, 2014 at 10:24 AM
Not go access to Unity at the moment but if the script is attached to the character you want to disable a component on then in c#
gameObject.GetComponent<NaveMeshAgent>().enabled = false;
JS
gameObject.GetComponent(NavMeshAgent).enabled = false;
if the script isn't on the character the you need to reference the gameObject it's on with Find or FindGameObjectWithTag.
The above should work just replace gameObject with whatever variable use store the result of the Find.
Like I said not got access to Unity so sorry for any typo's!
Thanks man that's the one I've been looking for! Great help!
No problem amazed there were no typos as the rest of the post is littered with them. Typing on a phone! You can use the same for any component of a gameObjet so if you have a script called health with a public method called TakeDamage that takes a float use get component for health and replace enabled with TakeDamage ( float 5f); and youve passed 5 point of damage to it.
how come it doesn't work for me? unity 5? cant seem to remove navmeshagent component.
Have you tried using UnityEngine.AI.Nav$$anonymous$$eshAgent ins$$anonymous$$d of just Nav$$anonymous$$eshAgent?
gameObject.GetComponent<UnityEngine.AI.Nav$$anonymous$$eshAgent>().enabled = false;
Your answer
Follow this Question
Related Questions
Finding Closest Object Through Navmesh,Identifying Which Object Is Closest on NavMesh 0 Answers
is it possible to use navmesh on flying character and target ? 0 Answers
My ai is getting stuck when there is a lot of them 0 Answers
Prevent NavMesh from Moving Diagonally? 1 Answer
Unity pathfinding - Comparing 2 paths? 2 Answers