Vector3 正在重置为 0,0,0 Unity

Vector3 is being reset to 0,0,0 Unity

我现在正在制作一款游戏,其中敌人会提醒玩家并无限期地追逐他们,直到玩家站在游戏中的“安全盘”上。当玩家站在这里时,敌人应该 return 到他们原来的位置。

问题是,每当玩家站在本垒上时,我都会收到空引用异常错误,并且我可以在控制台中看到原来的后卫位置已重置为 0,0,0。我假设空引用异常的原因是因为世界原点不在我正在使用的导航网格上,尽管我很容易在这一点上出错。我似乎无法弄清楚为什么 vector3 值发生了变化,因为 guardPosition 变量在 Start 上启动并且再也没有被触及过。

我已经包括了我的 enemyAi class(附加到敌人的脚本)和我的 class 与踩板相关。如果还需要包含任何内容,请告诉我。如果有人有任何想法,将不胜感激。干杯。

Screenshot on console after stepping on plate

public class EnemyAI : MonoBehaviour
{
    
    DeathHandler dh;

    [SerializeField] Transform target;
    [SerializeField] float chaseRange = 5f;
    [SerializeField] float killRange = 2f;

    [SerializeField] GameObject explosionEffect;

    NavMeshAgent navMeshAgent;
    float distanceToTarget = Mathf.Infinity;
    bool isDead = false;
    bool isAlert = false;

    Vector3 guardPosition;

    void Start()
    {
        GameObject gob;
        gob = GameObject.Find("Player");
        dh = gob.GetComponent<DeathHandler>();

        guardPosition = transform.position;

        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        distanceToTarget = Vector3.Distance(target.position, transform.position);

        print(guardPosition + " guard position during update");

        //alert a dude
        if (distanceToTarget <= chaseRange && !isDead)
        {
            isAlert = true;
        }

        //chase a dude
        if (isAlert == true)
        {
            ChasePlayer();
            print(isAlert);
        }
    }

    public void ChasePlayer()
    {
        navMeshAgent.SetDestination(target.position);

        //explode a dude
        if (distanceToTarget <= killRange)
        {
            Explode();
            dh.KillPlayer();
        }
    }

    public void SetAlertStatus(bool status)
    {
        isAlert = status;
    }

    public void ReturnToPost()
    {
        //isAlert = false;
        print(guardPosition + " guard position after stepping on plate");
        navMeshAgent.SetDestination(guardPosition);
    }    

    void Explode()
    {
        Instantiate(explosionEffect, transform.position, transform.rotation);

        isDead = true;
        Destroy(gameObject);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, chaseRange);
    }
}
public class SafeSpots : MonoBehaviour
{
    
EnemyAI enemyAi;

    

    void Start()
    {
        GameObject gob;
        gob = GameObject.FindGameObjectWithTag("Enemy");
        enemyAi = gob.GetComponent<EnemyAI>();
    }

    public void OnTriggerStay(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            

            enemyAi.SetAlertStatus(false);
            enemyAi.ReturnToPost();
        }
    }
}

您正在获取 EnemyAI 的禁用实例。相反,尽可能使用 FindGameObjectsWithTag then iterate through all of them and add their EnemyAI to a list which you can iterate through when necessary. By the way, it's better to use CompareTag 来减少垃圾:

public class SafeSpots : MonoBehaviour
{
    
    List<EnemyAI> enemyAis;

    void Start()
    {
        GameObject[] enemies= GameObject.FindGameObjectsWithTag("Enemy");
        enemyAis = new List<EnemyAI>();
       
        foreach (GameObject enemy in enemies)
        {
            enemyAis.Add(enemy.GetComponent<EnemyAI>());
        }
    }

    public void OnTriggerStay(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            foreach(EnemyAI enemyAi in enemyAis)
            {
                enemyAi.SetAlertStatus(false);
                enemyAi.ReturnToPost();
            }
        }
    }
}