问题
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