Find inactive GameObject by name, tag or layer

后端 未结 3 810
滥情空心
滥情空心 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:22

    In your case Invoke is a better option than a coroutine. Also, as far as I know you cannot .Find an inactive object, so you should assign the found object to a field.

    var obj;
    
    void BarDeactivate(){
        obj = GameObject.Find("OBJ");
        obj.SetActive(false);
    }
    
    void BarReactivate(){
        obj.SetActive(true);
    }
    

    Call them via Invoke like this:

    Invoke("BarDeactivate", sec);
    Invoke("BarReactivate", sec);
    
    0 讨论(0)
  • 2020-12-04 03:26

    I have this issue with ARCore, Vuforia, etc. because GameObjects are not alive until they can be tracked. One way is wait for the target to be recognized and then look for the GameObjects, but if they are manually inactive the only other (uncomplicated?) way would be to manually list them and link them as a public array with the script. If your script is linked then even an inactive GameObject will be accessible.

    0 讨论(0)
  • 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<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();
    }
    
    0 讨论(0)
提交回复
热议问题