Trouble with NavMesh AI.

I’ve been trying to create an enemy AI with navMesh so the enemy can follow the player when the player enters the enemies radius. however it’s given me errors when I play test and wont work at all, so I’m at a loss.

i have a navMesh set to the ground which is a terrain that also has a terrain collider which is set to static. The enemy has a navMeshAgent and the enemy and the agent are touching the ground and the navMesh.

when i play test it come up with these errors:

Failed to create agent because it is not close enough to the NavMesh
and
“SetDestination” can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination (UnityEngine.Vector3)
EnemyAI:Update () (at Assets/Enemies/EnemyAI.cs:25)

i don’t know what the first one means cause the agent is touching the navMesh so its a bit confusing.

here is the script that runs it;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyAI : MonoBehaviour
{
    public float visionRadius = 10f;

    Transform target;
    NavMeshAgent agent;

    void Start()
    {
        target = playerManager.instance.player.transform;
        agent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        float distance = Vector3.Distance(target.position, transform.position);

        if(distance <= visionRadius)
        {
            agent.SetDestination(target.position);

            if(distance <= agent.stoppingDistance)
            {
                FaceTarget();
            }
        }
    }

    void FaceTarget()
    {
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, visionRadius);
    }
}

any help to find what I’m missing would be much appreciated.

What version of Unity are you using?

Navmesh does not depend on physics collision, thus a terrain collider is of no help. NavMesh generates its own thing called a “NavMesh Surface”, which needs to be baked. In older versions, the NavMesh Surface is not within the scene, while in the latest version, it is a component to be placed in the scene.

Those errors are likely because you do not have a NavMesh surface, and therefore your NavMesh agent has no idea where to go.