0

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!

Community
  • 1
  • 1
Jack Godfrey
  • 23
  • 1
  • 4
  • 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 Dec 11 '19 at 08:53
  • Have you baked a navmesh? – Mark Davies Dec 11 '19 at 09:41
  • @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 Dec 11 '19 at 11:47
  • Is the prefab disabled by default? Is the gameobject you've instantiated enabled when you set its destination? – user14492 Dec 11 '19 at 12:49

3 Answers3

1

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.

Ben
  • 11
  • 1
0

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 Warp 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.

Evorlor
  • 7,263
  • 17
  • 70
  • 141
user14492
  • 2,116
  • 3
  • 25
  • 44
0

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.

rob1997
  • 176
  • 7
  • 1
    It is for certain baked when a game object is there before runtime then it works but if I spawn it afterwards it gives me the error – Jack Godfrey Dec 16 '19 at 09:38
  • 1
    Yep that's because you'll need to rebake it at runtime. The NavMeshSurface treats your spawned prefab as a brand new agent so it needs to be rebaked right before setting destination for new spawned agent. Here's a link for multiple ways on how https://stackoverflow.com/questions/52187167/how-to-bake-navmesh-in-runtime https://unity3d.com/fr/learn/tutorials/topics/navigation/baking-navmesh-runtime Good luck! – rob1997 Dec 16 '19 at 10:24