How do I create baked NavMesh after instantiate prefab at runtime?
Please some one help me how to code on this.
what I want to do achieve here are...
I have some stage prefabs, which includes enemy, stage, wall etc... it can instantiate once at every level.
these stages need to bake before or after instantiating in hierarchy in order to play game.
what I tried.
downloaded Navmesh component from Github.
try to code when I spawn prefab in hierarchy and then bake.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.AI;
public class NavMeshBakeScript : MonoBehaviour { [SerializeField] private NavMeshSurface[] _surface; private NavMeshSurface _stage; private NavMeshSurface _goal; private NavMeshSurface _wall;
private void Awake() { _stage = GameObject.FindWithTag("stage").GetComponent<NavMeshSurface>(); _goal = GameObject.FindWithTag("Goal").GetComponent<NavMeshSurface>(); _wall = GameObject.Find("wall").GetComponent<NavMeshSurface>(); if (_stage != null) _surface[0] = _stage; if (_goal == null) return; if (_wall == null) return; _surface[1] = _goal; _surface[2] = _wall; for (int i = 0; i < _surface.Length; i++) { _surface[i].BuildNavMesh(); } } }
however, I was not able to get component in the code array(_surface) and also didn't bake navmesh after spawning at runtime.
I am pretty confused about how to get reference from prefab and also coding... please some help me on this. I know I am a totally beginner.
thank you.
Your answer