0
votes

I am trying to make a zombie wave game and current have a Prefab for my enemies. If I have the prefab be in the scene when I hit run, they are attached to the NavMesh and track the player perfectly. I want to achieve this but with the enemy being spawned from an empty GameObject so I can get the waves spawning in. I have achieved them Spawning but they have the error,

"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
EnemyAI:Update() (at Assets/Scripts/EnemyAI.cs:25)

Here is my EnemyAI Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class EnemyAI : MonoBehaviour
{
    public float lookRadius = 10f;

    Transform target;
    NavMeshAgent agent;
    public GameObject Player;
    
    void Start() 
    {
        agent = GetComponent<NavMeshAgent>();
    }
    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(Player.transform.position, transform.position);
        
        if (distance <= lookRadius)
        {
            agent.SetDestination(Player.transform.position);
        }
    }
}

And my spawning script, which is attached to an empty game object,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawning : MonoBehaviour
{
    public GameObject prefab;
    public int CountofCubes;
    private IEnumerator coroutine;
    public float spawnRate;
    
    IEnumerator Start()
    {
        while (true)
        {
            for (int i = 0; i < CountofCubes; i++)
            {
                    Instantiate(prefab, new Vector3(Random.Range(-25.0f, 25.0f), 0.5f, Random.Range(-25.0f, 25.0f)), Quaternion.identity);
            }
            yield return new WaitForSeconds(spawnRate);
        }
    }   
} 

My enemy prefab which is being spawned

Any help would be great thanks!

3
get it generating cube without agents and check that the cubes appear correctly within the navmesh, as Im guessing its either below, or outside the navmesh area - BugFinder
Have you baked a navmesh? - Mark Davies
@MarkDavies Yes, I have a baked Mesh as I can have prefabs that are already in the scene on run will work and track the player, so the baked mesh works. Its when I want to spawn in the waves of enemies - Jack Godfrey
Is the prefab disabled by default? Is the gameobject you've instantiated enabled when you set its destination? - user14492

3 Answers

1
votes

I had the same issue and I don't have an explanation but only a workaround:

In the Start fucntion, I added:

navAgent = GetComponent<NavMeshAgent>();
navAgent.enabled = false;

// Invoke is used as a workaround for enabling NavMeshAgent on NavMeshSurface
Invoke("EnableNavMeshAgent", 0.025f);

And the EnableNavMeshAgent function is just :

private void EnableNavMeshAgent ()
{
    navAgent.enabled = true;
}

If I set the invoke delay to a value less than 0.025 second the error keep going but for 0.025 I only have it twice and the behaviour is the one I wanted after that.

0
votes

Some reasons this might happen:

  1. The agent is not on level with any navmesh i.e. can be too far above or below. You want it to be "close to the NavMesh surface". Use raycasting on the floor to position the agent or add a rigidbody and drop it from above the floor. After you do this you might need to disable and enable the agent.
  2. Moving the transform yourself rather than using Wrap function. There's property where you can check if the simulated and actual position are in sync.
  3. Corrupted navmesh so you might need to re-bake it.

It is essentially trying to tell you your agent is not on the mesh so it cannot find a path. Try playing with where you're placing the agent.

0
votes

I kind of remember running into a similar problem a while back and problem was I forgot to bake the NavMesh in Window > AI > Navigation. Just in case.