Find an inactive gameObject with tag?

人盡茶涼 提交于 2021-01-28 00:28:48

问题


Is there a way to find inactive gameObjects with a tag? There are answers saying no its not possible but that was in 2013. Any recent changes to do this?

 public GameObject[] FindWithTag;

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

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

}

回答1:


No. You still cannot find a disabled object with FindGameObjectsWithTag.

GameObject.FindGameObjectsWithTag - Manual:

Returns a list of active GameObjects tagged tag. Returns empty array if no GameObject was found.

As a secondary solution you can create an empty enabled object with a tag and then add the disabled object as child of it. Then you can retrieve what you need using GetComponentInChildren (that can find components also on disabled objects).

In that case you can do this:

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

    foreach (GameObject FindWithTags in FindWithTag)
    {
         var whatYouNeed = FindWithTags.GetComponentInChildren<YourComponent>(true);
         ...further logic...
    }
}

GetComponentInChildren can get components also on disabled object if you set its bool includeInactive parameter to true (by default it is false).



来源:https://stackoverflow.com/questions/57323175/find-an-inactive-gameobject-with-tag

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!