Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

Question '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:
    68
    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:
    411
unityunity