Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Help Wanted 'SetDestination' can only be called on an active agent that has been placed on a NavMesh.

Discussion in 'Scripting' started by JimJPlaysYT, Dec 3, 2021.

  1. JimJPlaysYT

    JimJPlaysYT

    Joined:
    Jan 16, 2021
    Posts:
    59
    I was following this tutorial
    https://www.bing.com/videos/search?...-20&sk=&cvid=01447386E6EA456CBC1D8E250CB06138
    I know its a bit out of date but I was just trying to get an ai to follow me. Here is my script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.AI;
    6.  
    7. public class EnemyController : MonoBehaviour
    8. {
    9.     public float lookRadius = 10f;
    10.  
    11.     Transform target;
    12.     NavMeshAgent agent;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         target = PlayerManager.instance.player.transform;
    18.         agent = GetComponent<NavMeshAgent>();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.         float distance = Vector3.Distance(target.position, transform.position);
    25.  
    26.         if (distance <= lookRadius)
    27.         {
    28.             agent.SetDestination(target.position);
    29.         }
    30.     }
    31.  
    32.     void OnDrawGizmosSelected()
    33.     {
    34.         Gizmos.DrawWireSphere(transform.position, lookRadius);
    35.     }
    36. }
    37.  
    The error code
    upload_2021-12-3_20-37-13.png
     
  2. Johan_Liebert123

    Johan_Liebert123

    Joined:
    Apr 15, 2021
    Posts:
    372
unityunity