查找带有标签的非活动gameObject?

时间:2019-08-02 09:02:10

标签: c# unity3d

有没有办法找到带有标签的不活跃游戏对象?有答案说不可能,但是那是在2013年。最近有什么改变吗?

 public GameObject[] FindWithTag;

void Update()
{
   FindWithTag = GameObject.FindGameObjectsWithTag("Player");

    foreach (GameObject FindWithTags in FindWithTag)
    {
     ...
    }

}

1 个答案:

答案 0 :(得分:3)

不。您仍然无法使用FindGameObjectsWithTag找到禁用的对象。

GameObject.FindGameObjectsWithTag -Manual

  

返回有效 GameObjects标记标签的列表。返回空数组   如果没有找到GameObject。

作为辅助解决方案,您可以创建带有标签的空的启用对象,然后将禁用的对象添加为其子对象。然后,您可以使用GetComponentInChildren检索需要的内容(可以在禁用的对象上找到组件)。

在这种情况下,您可以这样做:

void Update()
{
   FindWithTag = GameObject.FindGameObjectsWithTag("Player");

    foreach (GameObject FindWithTags in FindWithTag)
    {
         var whatYouNeed = FindWithTags.GetComponentInChildren<YourComponent>(true);
         ...further logic...
    }
}
如果将其GetComponentInChildren参数设置为true(默认值为false),

bool includeInactive也会在禁用对象上获取组件。