- Home /
... Can only be called on an Active Agent that has been placed on a Nav Mesh?
I have no errors before I start the game but these things usually pop up a minute into run time. I know these are coming from the scrips of an Ai unit i made with a Nav Mesh Agent component. the map is fully baked as well. However I have at least 50 of these units and I only get around 3-5 of these errors.
Here is the error message...
""SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:set_destination(Vector3)"
and here is the code where this error points to...
function inCombatAi()
{
if(DEBUG)Debug.Log(inCombat);
if(inCombat == false && targetObj == null && fireOnce == true)
{
if(DEBUG)Debug.Log("Finding Destination");
Nav.destination = FindClosestTarget().transform.position;
}
if(inCombat == true && targetMemory !== null && targetDes !== null && gameObject !== null && Nav !== null)
{
if(DEBUG)Debug.Log("Finding Target");
var target = targetDes.transform;
Nav.destination = target.position;
var angleCheck = Vector3.Angle(transform.forward, target.position - transform.position) < 15;
var distCheck = Nav.remainingDistance < range;
var distCheckTwo = Nav.remainingDistance >= range;tin
if(angleCheck && distCheck)
{
Nav.stoppingDistance = range * 0.8;
Nav.ResetPath();
}
if(!angleCheck && distCheckTwo)
{
Nav.stoppingDistance = range * 0.1;
Nav.ResetPath();
}
}
}
I read around the forums (REALLY awesome community btw) and someone said that its because the object isnt on the Nav mesh and a fix would be just aligning the object right on top of the Mesh.. however these errors pop up during run time and I believe something happens that causes the unit to maybe jump up. I dont really know. If someone can help point out a way ti fix this, I would really appreciate it!
Thanks in advance!!
i think i had this problem once, and it was down to the parent location of the prefab.
so what was happening was, the parent was somewhere random in the scene, even tho the prefab was spawning at the location. the parent threw the error as it was in a different location to where i thought it was.
I would suggest dragging the the prefab into the scene , and looking where each item is in relation to its child. i.e. click the top parent, note where that is, then child of it, then the next child etc, and see where the axis is of each is
Ah I see what your saying. In my case, the script was attached to a unit called "$$anonymous$$ion (clone)" which came from a spawner. There are at least 50 of these units in the scene with the same name. I think when the unit dies the nav mesh agent somehow still exists and relates itself to a new spawn of "$$anonymous$$ion (clone)" since they have the same name.
The $$anonymous$$ion only has two objects, its main body (parent) and a child. although the child doesnt have any scripts on it.
What do you think?
Answer by pragmar · Apr 28, 2015 at 03:32 AM
Old thread, I know--but just went through the same thing. There's an unintuitive sequence of events that cause this.
1. Destoy(something)
2. something.Update event
3. something.OnDestroy event
Fortunately OnDisable seems to fire regularly between Destroy and Update.
So far, this is working --
public class Something {
public NavMeshAgent agent;
public bool doUpdate;
public Vector3 dest;
void Start () {
agent = GetComponent<NavMeshAgent>();
doUpdate = true;
dest = Vector3.zero;
}
void Update () {
if (doUpdate)
{
// find dest
agent.SetDestination(dest);
}
}
void OnDisable()
{
doUpdate = false;
}
}
Answer by metars · Dec 02, 2015 at 04:07 PM
Don't forget scene floor always must be static for NavMeshAgent
Your answer
Follow this Question
Related Questions
Nav Mesh Agents Not working 0 Answers
Nav Mesh Agent 0 Answers
NavMesh obstacle avoidance faulty? 1 Answer
My Nav Mesh Agent won't touch its target. 2 Answers
Why Nav Mesh Agent has been baked intermittently on 1 straight component? 0 Answers