chevron_left

Problem :

I am new to Unity5. I am facing the following error while trying to do setDestination.

"SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.NavMeshAgent:SetDestination(Vector3) CompleteProject.EnemyMovement:Update() (at Assets/_CompletedAssets/Scripts/Enemy/EnemyMovement.cs:30)

Please find below my code for your reference.

using UnityEngine;
using System.Collections;
namespace MyCompleteProject
{
    public class MyEnemyMovement : MyMonoBehaviour
    {
        Transform players;              
        PlayerHealth playerHealths;     
        EnemyHealth enemyHealths;       
        NavMeshAgent navs;              
         void Awake ()
        å{
         players = GameObject.FindGameObjectWithTag        ("Player").transform;
            playerHealths = players.GetComponent <PlayerHealth>  ();
            enemyHealths = GetComponent <EnemyHealth> ();
            navs = GetComponent <NavMeshAgent> ();
        }
       void Update ()
        {
            if(enemyHealths.currentHealths > 0 &&     playerHealths.currentHealths > 0)
            {
              navs.SetDestination (players.position);
            }

            else
            {
               navs.enabled = false;
            }
        }
    }
}

May I know what is wrong with the above code? Does anyone have any solution to my issue?

Solution :

I have lots of industry experience in writing complex Unity5 code. I have gone through your post and understood your problem.

So to begin with you must follow the below approach to resolve your issue:

1. Please make sure that your thirdPersonController is above the ground.
2. Please go to the Window->Navigation->Bake and click on bake.

The blue area is where the third person can reach the ground.

Another Solution:

STEP 1: You need to add a Navmesh to your scene before you can use NavMeshAgent or anything else related to Navigation.
STEP 2: Ensure that your thirdPersonController is above the ground.
STEP 3: .Go to Window->Navigation->Bake and click bake.The blue area is where the thirdperson can reach in the ground.
STEP 4: Your floor (plane or quad) must be static.
STEP 5: Add NavmeshAgent component your player or your enemy.

You can find a static checkbox on the inspector panel

STEP 6:
If you create a NavMeshAgent and set its position via transform.position=... and then try to SetDestination, it fails because the NavMeshAgent did not recognize the position change and does not know that it already is on the NavMesh. Use NavMeshAgent.Warp instead of transform.position to initialize the position before calling SetDestination.

STEP 7:
set up some params (The tutorial has Radius=0.75, Height=1.2 and Step Height=.0.1, and the rest of the settings from the tutorial are gone in Unity 5, as far as I can tell.

STEP 8:
The following code will solve this problem

nav.enabled = false;

More Posts

How can I switch between two cameras after a rotation of an object has finished? Tushar Shuvro - Jul 4, 2020
Program code that can be evaluated to a value is called a(n Lalit Kumar - Sep 17, 2020
When a program lets the user know that an invalid choice has been made, this is known as; Farkhanda Athar - Aug 24, 2020
error 1075 the dependency service does not exist or has been marked for deletion SharadMagar450 - Sep 17
Given an int variable k that has already been declared DivyaN - Oct 22
Cannot be accessed with an instance reference, qualify it with a type name instead Alecxe01 - Nov 12
[Javascript]Assume that x is a char variable that has been declared and already given a value. DivyaN - Oct 11
Is it possible to create a while loop that determines when the end of a file has been reached. Farkhanda Athar - Apr 11, 2020
Write a statement to set the value of num to 4 (num is a variable that has already been declared). Farkhanda Athar - Apr 11, 2020
typeerror: 'append' called on an object that does not implement interface formdata. kunal verma - Nov 8, 2020