Why am I getting "Failed to create agent because it is not close enough to the NavMesh"?

Hello everyone,
I am having some trouble with my NavMesh. I have an object that I am trying to get to path find, however, I get this error as soon as my game starts “Failed to create agent because it is not close enough to the NavMesh”. But it is directly on top of the navmesh ( I tried to upload a picture, but it said the file size is too large). I have tried deleting the agent component from the object and adding it in the script right before he moves, but when I do this, he doesn’t move. I have also tried creating the agent component in the start method, but this causes the object to warp to a different spot on the map. I am at a complete loss and would really appreciate any help. This is the code I am trying to use to make him move. when I tried to use the pathfinding I comment out the MoveTowards function.

public bool Move(Vector3 pos, float speed, GameObject unit)
    {
        bool isMoving = true;

        if(unit.transform.position == targetPosition)
        {
            isMoving = false;
            return (isMoving);
        }
        else
        {
            unit.transform.position = Vector3.MoveTowards(unit.transform.position, targetPosition, speed * Time.deltaTime);
            //unit.GetComponent<NavMeshAgent>().speed = speed * Time.deltaTime;
            //unit.GetComponent<NavMeshAgent>().SetDestination(targetPosition);
            return (isMoving);
        }
    }

I had that error when I was using a Prefab. Unity was checking the Prefab and the Prefab wasn't on the NavMesh. You can test this by setting a public value and then checking in the inspector whether your object in the scene changes that value or the prefab changes that value. I think the solution was to instantiate the object with a reference - and then use that reference like you're using "unit".

Here is an example - when I call the MoveNow() function from another script the agent will start moving towards the destination. public Transform goal; NavMeshAgent unit; private void Start() { unit = GetComponent<NavMeshAgent>(); } public void MoveNow() { unit.destination = goal.position; }

1 Answer

1

Okay so I have an update, I used NavMeshAgent.Warp() to move him the to location and it worked. However, once he had been moved to the target location, I got the “Failed to create agent because it is not close enough to the navmesh” message again. I am using a click to move function as I am trying to create a 3d turn-based tactics game. I use raycasting to get the location that was clicked on the screen. I’m not sure if that is causing this or what. @Janette5 thank you for helping me so far, I think I am starting to understand what is going on. But I am still unsure as to how to fix it.