0

我正在制作一个带有 Enemy 定义的 SO,如下所示:

 public class Enemy_SO : ScriptableObject
 {
     public string enemyName;
     [Header("AI Agent Settings")]
     public int speed;
     public int angularSpeed;
 }

我将它附加到 Enemy GameObject 上,如下所示:

 public class EnemyController : MonoBehaviour
 {
       public Enemy_SO enemy_1;
       public NavMeshAgent enemyAgent;

  private void Start()
     {
         enemyAgent = GetComponent<NavMeshAgent>();
         Debug.Log("name: " + enemy_1.name);
         enemyAgent.speed = enemy_1.speed;
     }
 }

在播放控制台输出时:

 NullReferenceException: Object reference not set to an instance of an object
 EnemyController.Start () (at Assets/Scripts/Enemies/EnemyController.cs:29)

 name: Enemy 1
 UnityEngine.Debug:Log(Object)
 EnemyController:Start() (at Assets/Scripts/Enemies/EnemyController.cs:29)

所以它在第 29 行抛出了一个空引用,但随后它正确地输出了第 29 行,并且所有代理变量在播放期间都在检查器中正确设置。有没有办法摆脱错误?我只是忽略它吗?

4

0 回答 0