Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
460 views
in Technique[技术] by (71.8m points)

c# - Find inactive GameObject by name, tag or layer

First, I need to deactivate a game object and then after 10 seconds, activate it, so I thought coroutines are suitable:

IEnumerator BarDeactivate(float sec)
{
        yield return new WaitForSeconds(sec);

        var obj = GameObject.Find("OBJ");
        obj.SetActive(false);
}

IEnumerator BarReactivate(float sec)
{
        yield return new WaitForSeconds(sec);

        var obj = transform.Find("OBJ");
        obj.SetActive(true);
}

Obviously, I can no longer use GameObject.Find so I use transform.Find to find the inactive game object, but of course .SetActive now does not work, since obj is not actually a game object...

How do I cast the found transform, so that it can be set active again?

I tried obj.gameObject.SetActive(true) but it must be wrong, because the object would not come back to life...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that Unity cannot find inactive GameObjects. GameObject.Find will only find active GameObject. You should either find and store the GameObject in a global variable or make the variable public then assign it from the Editor.

My solution uses a global variable then stores the GameObject in the beginner so that you don't have to look for it again.

GameObject obj;

void Start()
{
    obj = GameObject.Find("OBJ");
}
IEnumerator BarDeactivate(float sec)
{
    yield return new WaitForSeconds(sec);
    obj.SetActive(false);
}

IEnumerator BarReactivate(float sec)
{
    yield return new WaitForSeconds(sec);
    obj.SetActive(true);
}

Below is a wrapper I made that finds GameObjects by name, tag or layer even if they are inactive.

You shouldn't be using these every frame because they are slow. They are good if used in the Start or Awake function.

Find one GameObject:

USAGE:

void Start()
{
    GameObject objByName = FindInActiveObjectByName("Cube");
    GameObject objByTag = FindInActiveObjectByTag("CubeTag");
    GameObject objByLayer = FindInActiveObjectByLayer(LayerMask.NameToLayer("CubeLayer"));
}

Find in-active GameObject by Name:

GameObject FindInActiveObjectByName(string name)
{
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].name == name)
            {
                return objs[i].gameObject;
            }
        }
    }
    return null;
}

Find in-active GameObject by Tag:

GameObject FindInActiveObjectByTag(string tag)
{

    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].CompareTag(tag))
            {
                return objs[i].gameObject;
            }
        }
    }
    return null;
}

Find in-active GameObject by Layer:

GameObject FindInActiveObjectByLayer(int layer)
{

    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.layer == layer)
            {
                return objs[i].gameObject;
            }
        }
    }
    return null;
}


Find all GameObjects (Notice the "s" in the Object from all the function names below):

USAGE:

void Start()
{
    GameObject[] objByNames = FindInActiveObjectsByName("Cube");
    GameObject[] objByTags = FindInActiveObjectsByTag("CubeTag");
    GameObject[] objByLayers = FindInActiveObjectsByLayer(LayerMask.NameToLayer("CubeLayer"));
}

Find in-active GameObject[s] by Name:

GameObject[] FindInActiveObjectsByName(string name)
{
    List<GameObject> validTransforms = new List<GameObject>();
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.name == name)
            {
                validTransforms.Add(objs[i].gameObject);
            }
        }
    }
    return validTransforms.ToArray();
}

Find in-active GameObject[s] by Tag:

GameObject[] FindInActiveObjectsByTag(string tag)
{
    List<GameObject> validTransforms = new List<GameObject>();
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.CompareTag(tag))
            {
                validTransforms.Add(objs[i].gameObject);
            }
        }
    }
    return validTransforms.ToArray();
}

Find in-active GameObject[s] by Layer:

GameObject[] FindInActiveObjectsByLayer(int layer)
{
    List<GameObject> validTransforms = new List<GameObject>();
    Transform[] objs = Resources.FindObjectsOfTypeAll<Transform>() as Transform[];
    for (int i = 0; i < objs.Length; i++)
    {
        if (objs[i].hideFlags == HideFlags.None)
        {
            if (objs[i].gameObject.layer == layer)
            {
                validTransforms.Add(objs[i].gameObject);
            }
        }
    }
    return validTransforms.ToArray();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...