- Home /
Prefabs and NavMesh
Hi, I am trying to make a small RTS game to sort of make an idea. I am trying to control a "Player" and this works when it isn't a prefab. Once i add in 1 prefab or mutiple is doens't work anymore and it gives a error with the NavMesh.
This is the error: "SetDestination" can only be called on an active agent that has been placed on a NavMesh. UnityEngine.AI.NavMeshAgent:set_destination(Vector3) APLayerController:Update() (at Assets/APLayerController.cs:35)
This is my scripts: using UnityEngine; using UnityEngine.AI;
public class APLayerController : MonoBehaviour {
public Camera _cam;
public Transform player;
public NavMeshAgent _playerMesh;
private float selectDistance = 100f;
public bool playerControl = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
RaycastHit hit;
if (Input.GetMouseButtonDown(0) && playerControl == false) {
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && playerControl == false) {
if (hit.collider.gameObject.name == "Player" && playerControl == false) {
playerControl = true;
}
}
}
if ((Input.GetMouseButtonDown(0)) && (playerControl)) {
Ray ray = _cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit) && (playerControl)) {
if (hit.collider.gameObject.name == "Ground" && (playerControl)) {
_playerMesh.destination = hit.point;
}
}
}
if (Input.GetButton("Fire3") && playerControl) {
playerControl = false;
Debug.Log("Error");
}
}
}
Answer by butterfly42 · Sep 30, 2020 at 10:06 AM
The Code doesn't really matter for this problem. The NavMesh Is usually created for a completed scene on static objects, but if the envrionments become prefabs this process can run into problems.
Try using the NavMeshComponents to create a dedicated NavMesh to your prefab.
Your answer
Follow this Question
Related Questions
Multiple navMeshes? (swapping navMeshs at runtime) 0 Answers
Can you bake nav mesh in prefab mode? 0 Answers
Navmesh Trouble 1 Answer
Need help detecting barriers for my game 0 Answers