Find inactive GameObject by name, tag or layer

后端 未结 3 828
滥情空心
滥情空心 2020-12-04 02:49

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)
         


        
3条回答
  •  情话喂你
    2020-12-04 03:34

    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() 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() 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() 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 validTransforms = new List();
        Transform[] objs = Resources.FindObjectsOfTypeAll() 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 validTransforms = new List();
        Transform[] objs = Resources.FindObjectsOfTypeAll() 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 validTransforms = new List();
        Transform[] objs = Resources.FindObjectsOfTypeAll() 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();
    }
    

提交回复
热议问题