Skip to content

Nav Agent doesn't find the mesh when programatically instantiated #169

@drizztdourden08

Description

@drizztdourden08

Hi,

I want to report a potential issue when spawning nav agents on an existing mesh.

As a reference, I did open the following issue (To which I couldn't respond for a long times due to special circumstances, sorry for that): #154

After a lot of troubleshooting and nothing really making any sense, It worked in another scene where I replicated the demo, but then I saw it didn't when I programmatically spawned navAgent on the mesh, returning an error message saying it didn't find a close enough mesh. I did find a workaround by disabling the nav agent component and re-enabling it in the "start" function. it then finds the "closest mesh" and works normally from there.

All these times, I thought my mesh wasn't good and that I wasn't refreshing it properly but It was just fine.

So yeah. I think the nav agent might try to find the mesh before it's actually ready and thus, doesn't find it. I'm not sure exactly.

Just as reference, the code for me enemy that uses the mesh:

        private void Awake() {
            tag = "Enemy";

            nav = GetComponent<NavMeshAgent>();
            nav.updateRotation = false;
            nav.updateUpAxis = false;
            nav.speed = speed;

            if (layers.HasFlag(Layer.Air))
                nav.baseOffset = 5.0f;
        }

        private void Start() {
            nav.enabled = true;
        }

and just in case the way I instantiate it is important, here's the code:

        public static GameObject InstantiateFromPrefab(GameObject prefab, Vector3? position, Quaternion? rotation, Transform parent, string newName) {
            if (prefab == null) return null;

            GameObject instantiatedObject = Object.Instantiate(prefab);
            if (position.HasValue) {
                instantiatedObject.transform.position = position.Value;
            }
            if (rotation.HasValue) instantiatedObject.transform.rotation = rotation.Value;
            if (parent != null) instantiatedObject.transform.SetParent(parent, false);
            if (newName != null) instantiatedObject.name = newName;

            return instantiatedObject;
        }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions