Home > Net >  I don't know how to do
I don't know how to do

Time:10-14

this is my error

Assets\Standard Assets\Characters\ThirdPersonCharacter\Scripts\AICharacterControl.cs(7,31): 
error CS0118:'NavMeshAgent' is a namespace but is used like a type

This is my code I can't fix that :(

using System;
using UnityEngine;


namespace UnityStandardAssets.Characters.ThirdPerson.NavMeshAgent
{
    [RequireComponent(typeof (NavMeshAgent))]
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class AICharacterControl : MonoBehaviour
    {
        public NavMeshAgent agent { get; private set; } 
        public ThirdPersonCharacter character { get; private set; } 
        public Transform target; 

       
        private void Start()
        {
            
            agent = GetComponentInChildren<NavMeshAgent>();
            character = GetComponent<ThirdPersonCharacter>();

            agent.updateRotation = false;
            agent.updatePosition = true;
        }


       
        private void Update()
        {
            if (target != null)
            {
                agent.SetDestination(target.position);

                
                
                
                character.Move(agent.desiredVelocity, false, false);
            }
            else
            {
                
                character.Move(Vector3.zero, false, false);
            }

        }


        public void SetTarget(Transform target)
        {
            this.target = target;
        }
    }
}

thank you

CodePudding user response:

Don't use the names of existing types as namespaces. You have a custom namespace here:

namespace UnityStandardAssets.Characters.ThirdPerson.NavMeshAgent

So anything within that namespace (or in code which references the enclosing namespace) which refers to NavMeshAgent will be referring to the namespace. Not to the type in Unity.

Rename your namespace. For example:

namespace UnityStandardAssets.Characters.ThirdPerson.MyNavMeshAgent

(Or something more contextually meaningful.)

Basically the lesson here is that names matter. The names of your namespaces, types, variables, etc. should be clear and unambiguous to both you and to the compiler.


And, of course, to reference that type directly you'll need a using directive:

using UnityEngine.AI;
  • Related