Error An object reference is required for the non-static field, method

Hi, I’m getting this error when entering the NavMesAgent code.

An object reference is required for the non-static field, method, or property ‘NavMeshAgent.SetDestination(Vector3)’

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

public class EnemyAI : MonoBehaviour
{
    [SerializeField] Transform target;
    NavMeshAgent navMeshAgent;

    // Start is called before the first frame update

    void Start()
    {
        navMeshAgent = GetComponent<NavMeshAgent>();  
    }

    // Update is called once per frame
    void Update()
    {
        NavMeshAgent.SetDestination(target.position); // <-- Error on this line
    }

}

Use the instance of the component, navMeshAgent.

NavMeshAgent is the class, navMeshAgent is the instance of your agent.

Yes I see it now, thank you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms