如何在运行时检查 FindGameObjectsWithTag?

How can I check FindGameObjectsWithTag during runtime?

我的敌人 NavMeshAgent AI 有问题,首先它搜索标记为 "Defenses" 的游戏对象,然后我根据敌人的防御距离设置第一个目的地,当防御是销毁,数组值加一,将目的地更改为下一个防御。

我的问题是,我的玩家可以在游戏中创建(实例)新防御,当我所有的防御都被摧毁时,我的敌人会变得疯狂,所以,我需要一种方法将这些新防御添加到我的阵列中,如下所示是我的敌人脚本。

 public class NavMeshEnemy : MonoBehaviour
 {
     [Header("References", order = 0)]
     [SerializeField] NavMeshAgent enemyAgent;
     [Space(10)]
     [SerializeField] GameObject[] destinations;
     [Space(10)]
     [SerializeField] float distanceObjects;
     [SerializeField] int arrayElements = 0;

     void Awake()
     {
         enemyAgent = GetComponent<NavMeshAgent>();
         destinations = GameObject.FindGameObjectsWithTag("Defenses");
     }

     void Start()
     {
         destinations = destinations.OrderBy((d) => (d.transform.position - this.transform.position).sqrMagnitude).ToArray();
     }

     void Update()
     {
         distanceObjects = Vector3.Distance(this.transform.position, destinations[arrayElements].transform.position);

         enemyAgent.destination = destinations[arrayElements].transform.position;

         CheckArray();
     }

     void CheckArray()
     {
         if (destinations[arrayElements].gameObject.activeInHierarchy == false)
         {
             if (arrayElements < destinations.Length - 1)
             {
                 arrayElements++;
                 enemyAgent.destination = destinations[arrayElements].transform.position;
             }
             else
             {
                 arrayElements = 0;
             }
         }
     }
 }

感谢阅读! :)

我宁愿使用 static 列表在您的预制件上实现一个组件,例如

public class NavMeshDestination : MonoBehaviour
{
    public static HashSet<Transform> existingDestinations = new HashSet<Transform>();

    private void Awake()
    {
       if(!existingDestinations.Contains(this)) existingDestinations.Add(this);
    }

    private void OnDestroy()
    {
        if(existingDestinations.Contains(this)) existingDestinations.Remove(this);
    }
}

然后甚至不按标签行事,只做

public class NavMeshEnemy : MonoBehaviour
{
    [Header("References", order = 0)]
    [SerializeField] NavMeshAgent enemyAgent;
    [Space(10)]
    [SerializeField] float distanceObjects;
    [SerializeField] int arrayElements = 0;

    void Awake()
    {
        if(!enemyAgent) enemyAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        destinations = NavMeshDestination.existingDestinations.OrderBy(d => (d.transform.position - this.transform.position).sqrMagnitude).ToArray();

        distanceObjects = Vector3.Distance(this.transform.position, destinations[arrayElements].transform.position);

        enemyAgent.destination = destinations[arrayElements].transform.position;

        CheckArray();
    }

    void CheckArray()
    {
        if (destinations[arrayElements].gameObject.activeInHierarchy == false)
        { 
            if (arrayElements < destinations.Length - 1)
            {
                arrayElements++;
                enemyAgent.destination = destinos[arrayElements].transform.position;
            }
            else
            {
                arrayElements = 0;
            }
        }
    }
}

但是请注意,这仍然无法解决您没有可用目的地的问题。所以如果数组是空的,你可能应该停止执行,比如

if(NavMeshDestination.existingDestinations.Count == 0)
{
    // Stop the enemyAgent
    return;
}