unity设置敌人出生时报错

报错内容1
Failed to create agent because there is no valid NavMesh
UnityEngine.Object:Instantiate(Object, Vector3, Quaternion)
EnemySpawn:Update() (at Assets/Scripts/EnemySpawn.cs:28)

Transform obj = (Transform)Instantiate(m_enemy,m_transform.position,Quaternion.identity);

报错内容2
"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.NavMeshAgent:SetDestination(Vector3)
Enemy:Update() (at Assets/Scripts/Enemy.cs:59)

m_agent.SetDestination(m_player.transform.position);

现在又出现这个错误
NullReferenceException: Object reference not set to an instance of an object
Enemy.Update () (at Assets/Scripts/Enemy.cs:83

if (stateInfo.nameHash == Animator.StringToHash ("Base Layer.death") && ! m_ani.IsInTransition (0)) 
        {
            if(stateInfo.normalizedTime>=1.0f)        
            {
                m_spawn.m_enemyCount--;   //出错的是这一行
                GameManager.Instance.SetScore(100);
                Destroy(this.gameObject);
            }
        }

这个怎么处理

你最后一条报错是报空,因为你的m_spawn为空所以读不出count属性,检查下是否初始化,如果初始化了检查下哪里赋空值了

又是你,肯定又是变量没赋值的问题

需要先将 Navmesh 添加到场景中
也就是先判断你的游戏场景已经加载完成
点击窗口(Window) -> 导航(Navigation)
点击烘焙(Bake)
点击右下角的烘焙(Bake)

不懂这个,查的不知道对不对


1.Make sure that your thirdPersonController is above the ground.

2.Go to Window->Navigation->Bake and click bake.The blue area is where the thirdperson can reach in the ground


You need to add a Navmesh to your scene before you can use NavMeshAgent or anything else related to Navigation.

Click on the entrance first, then select Navigation - Object and check the Navigation Static box. After that do the same to the underground level and bake. Hope this helps!

So have you actually set up stuff in your scene as navigation static, and baked a navmesh?

haha lol seems like i forgot to bake bruh

你这个贴的代码和你报错的也不一样啊