- Home /
Checking if a gameobjects active in your heirarchy.
So I'm aware that you can use the function "GameObject.activeInHeirarchy" to check if the GameObject's active in scene but I don't think I've quite grasped the concept.
I'm building a tower defense game and need to check to see if an instance of the tower menu object has been created before i allow my user to accidentally create another one before finishing with the first one that was instantiated.
void OnGUI() {
if (mouseclick)
{
if (menu.activeInHierarchy == false)
{
if (Event.current.type == EventType.MouseUp)
{
Instantiate (menu, transform.position, Quaternion.identity);
}
}
else
{
}
}
}
Picture example below:
My turf areas are colliders which trigger the menu objects instantiating on mouseclick down. What am I missing?
Simplified: I'd like the game to not be able to make another menu if theirs a menu already present. Thankyou! Any help much appreciated.
Answer by Haxxxxx · Apr 08, 2014 at 02:33 AM
I completely changed my thinking and tried something else, solution down below:
public GameObject[] checkmenu;
bool mouseclick = false;
void menuCheck ()
{
checkmenu = GameObject.FindGameObjectsWithTag("Menu");
}
void OnGUI() {
if (mouseclick)
{
menuCheck();
if (checkmenu.Length == 0)
{
//Debug.Log("Array is empty");
if (Event.current.type == EventType.MouseUp)
{
Instantiate (menu, transform.position, Quaternion.identity);
}
}
else if (checkmenu.Length == 1)
{
//Debug.Log("Menu present");
}
}
}
Answer by MasterChiefLegend · Apr 08, 2014 at 12:41 AM
I would probably approach this differently, managing the state of the game from an explicit variable like isMenuInUse and when a menu item is chosen set it to false (or raise an event that sets it to false as appropriate).
If you must use the activeInHiearchy, I suspect you have an inactive parent gameobject? Try using activeSelf?