Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Adding using UnityEngine.AI breaks my script!

Discussion in 'Getting Started' started by tombedorchestra, Nov 2, 2020.

  1. tombedorchestra

    tombedorchestra

    Joined:
    Nov 1, 2020
    Posts:
    3
    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:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4.  
    5. namespace UnityStandardAssets.Characters.ThirdPerson
    6. {
    7.      [RequireComponent(typeof (NavMeshAgent))]
    8.      [RequireComponent(typeof (ThirdPersonCharacter))]
    9.      public class AICharacterControl : MonoBehaviour
    10.      {
    11.          public UnityEngine.AI.NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
    12.          public ThirdPersonCharacter character { get; private set; } // the character we are controlling
    13.          public Transform target;                                    // target to aim for
    14.  
    15.  
    16.          private void Start()
    17.          {
    18.              // get the components on the object we need ( should not be null due to require component so no need to check )
    19.              agent = GetComponentInChildren<NavMeshAgent>();
    20.              character = GetComponent<ThirdPersonCharacter>();
    21.  
    22.              agent.updateRotation = false;
    23.              agent.updatePosition = true;
    24.          }
    25.  
    26.  
    27.          private void Update()
    28.          {
    29.              if (target != null)
    30.                  agent.SetDestination(target.position);
    31.  
    32.              if (agent.remainingDistance > agent.stoppingDistance)
    33.                  character.Move(agent.desiredVelocity, false, false);
    34.              else
    35.                  character.Move(Vector3.zero, false, false);
    36.          }
    37.  
    38.  
    39.          public void SetTarget(Transform target)
    40.          {
    41.              this.target = target;
    42.          }
    43.      }
    44. }
     
  2. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,663
    Try this:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3. using UnityEngine.AI;
    4.  
    5. namespace UnityStandardAssets.Characters.ThirdPerson
    6. {
    7.      [RequireComponent(typeof (NavMeshAgent))]
    8.      [RequireComponent(typeof (ThirdPersonCharacter))]
    9.      public class AICharacterControl : MonoBehaviour
    10.      {
    11.          public NavMeshAgent agent { get; private set; }             // the navmesh agent required for the path finding
    12.          public ThirdPersonCharacter character { get; private set; } // the character we are controlling
    13.          public Transform target;                                    // target to aim for
    14.  
    15.  
    16.          private void Start()
    17.          {
    18.              // get the components on the object we need ( should not be null due to require component so no need to check )
    19.              agent = GetComponentInChildren<NavMeshAgent>();
    20.              character = GetComponent<ThirdPersonCharacter>();
    21.  
    22.              agent.updateRotation = false;
    23.              agent.updatePosition = true;
    24.          }
    25.  
    26.  
    27.          private void Update()
    28.          {
    29.              if (target != null)
    30.                  agent.SetDestination(target.position);
    31.  
    32.              if (agent.remainingDistance > agent.stoppingDistance)
    33.                  character.Move(agent.desiredVelocity, false, false);
    34.              else
    35.                  character.Move(Vector3.zero, false, false);
    36.          }
    37.  
    38.  
    39.          public void SetTarget(Transform target)
    40.          {
    41.              this.target = target;
    42.          }
    43.      }
    44. }
    45.  
    Make sure your character has a
    Nav Mesh Agent
    component
    added to it.
    Also, check the
    Console
    when dealing with errors.
     
    Last edited: Nov 2, 2020
  3. tombedorchestra

    tombedorchestra

    Joined:
    Nov 1, 2020
    Posts:
    3
    @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!
     

    Attached Files:

    • asdf.JPG
      asdf.JPG
      File size:
      69.6 KB
      Views:
      305
  4. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,663
    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.
     
    Last edited: Nov 2, 2020
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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.
     
  6. tombedorchestra

    tombedorchestra

    Joined:
    Nov 1, 2020
    Posts:
    3
    @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!