Adding using UnityEngine.AI breaks my script!

Completely new to Unity here. I have some coding experience.

I imported an asset pack and it came up with a bunch of errors. I fixed all but one of them which is this:

“…AICharacterControl.cs(6,31); error CS0246: The type or namespace name ‘NavMeshAgent’ could not be found…” etc …

So I applied “using UnityEngine.AI;” to the top of that script and it created about 20 more compile errors that I can’t find.

Does anyone know why it may have caused MORE errors… and how do I fixed it so I can enter game mode?

Thank you!

EDIT: I was able to get rid of ONE of the 2 errors by replacing ‘NavMeshAgent’ with “UnityEngine.AI.NavMeshAgent”. But now I still have another error saying the same thing. Here’s the whole code:

using System;
using UnityEngine;


namespace UnityStandardAssets.Characters.ThirdPerson
{
     [RequireComponent(typeof (NavMeshAgent))]
     [RequireComponent(typeof (ThirdPersonCharacter))]
     public class AICharacterControl : MonoBehaviour
     {
         public UnityEngine.AI.NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
         public ThirdPersonCharacter character { get; private set; } // the character we are controlling
         public Transform target;                                    // target to aim for


         private void Start()
         {
             // get the components on the object we need ( should not be null due to require component so no need to check )
             agent = GetComponentInChildren<NavMeshAgent>();
             character = GetComponent<ThirdPersonCharacter>();

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


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

             if (agent.remainingDistance > agent.stoppingDistance)
                 character.Move(agent.desiredVelocity, false, false);
             else
                 character.Move(Vector3.zero, false, false);
         }


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

Try this:

using System;
using UnityEngine;
using UnityEngine.AI;

namespace UnityStandardAssets.Characters.ThirdPerson
{
     [RequireComponent(typeof (NavMeshAgent))]
     [RequireComponent(typeof (ThirdPersonCharacter))]
     public class AICharacterControl : MonoBehaviour
     {
         public NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
         public ThirdPersonCharacter character { get; private set; } // the character we are controlling
         public Transform target;                                    // target to aim for


         private void Start()
         {
             // get the components on the object we need ( should not be null due to require component so no need to check )
             agent = GetComponentInChildren<NavMeshAgent>();
             character = GetComponent<ThirdPersonCharacter>();

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


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

             if (agent.remainingDistance > agent.stoppingDistance)
                 character.Move(agent.desiredVelocity, false, false);
             else
                 character.Move(Vector3.zero, false, false);
         }


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

Make sure your character has a Nav Mesh Agent component added to it.
Also, check the Console when dealing with errors.

@Mauri Thanks for the reply… that didn’t work. Adding ‘using UnityEngine.AI;’ creates more compile errors as shown in the picture. I also tried doing your code but NOT adding ‘using UnityEngine.AI;’ and it still did not work.

Other thoughts? Thanks!

The errors shown in your screenshot are not related to your issue/script, though.

Here’s the thing: The Standard Assets are pretty much outdated. They were once made for Unity 2018.4 and won’t work in newer versions without manual fixing, as e.g. some APIs have changed. You should be fine removing the Effects folder and use the Post Processing Stack instead.

This won’t fix the TargetFieldOfView.cs error, however. You may try replacing every ParticleSystem bit with ParticleSystemRenderer then.

If you just need the Characters stuff, keep the ‘Characters’ folder and remove everything else from the Standard Assets folder that’s not connected to their functionality.

1 Like

Yep, the Standard Assets package is no longer supported. To continue using it in current versions of Unity you have to self support.

But on this comment, when a code compile runs and hits a compile error, the compile stops. So fixing compile errors can result in revealing “new” compile errors, simply because the previous compile never got far enough to get to these new errors. This is normal behavior when you have several different scripts with code which will cause compile errors.

@Joe-Censored
@Mauri

Ahhh ok… this makes sense. Thank you all. I just went and deleted those files (after making a back-up) and it solved the problem!