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();
}