Search Unity

Check if multiple gameobjects are active

Discussion in 'Scripting' started by will_brett, Oct 31, 2014.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Hey there, Im currently writing a script that checks if multiple gameobjects are not active. If any are active I want it to keep checking if there are all inactive I want to do something else.

    So far I have: (I know this checks if they are active not inactive which is wrong)

    Code (CSharp):
    1. while (cleanBool == false)
    2.         {
    3.             //Needs to be inactive not active CHANGE!!!
    4.             if(check01.activeInHierarchy && check02.activeInHierarchy && check03.activeInHierarchy && check04.activeInHierarchy && check05.activeInHierarchy)
    5.             {
    6.                 cleanBool = true;
    7.             }
    8.             else
    9.             {
    10.                 return null;
    11.             }
    12.         }
    Is this a terrible way of doing it? Or is there a better way?

    Thanks
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    Well ideally you don't want to keep checking forever if all the gameObjects are active/inactive.

    I would only check once one of the gameObject becomes inactive by using OnDisable().

    Quick Example

    This would be the parent object which contains the script with the function that checks if all objects are inactive.
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CheckIt : MonoBehaviour {
    5.  
    6.     public GameObjScript[] gameObjArray;
    7.  
    8.     public void Start()
    9.     {
    10.         PopulateGameObjArray();
    11.     }
    12.  
    13.     void PopulateGameObjArray()
    14.     {
    15.         gameObjArray = GetComponentsInChildren<GameObjScript>();
    16.     }
    17.  
    18.     public void CheckObjStatus()
    19.     {
    20.         if(AreAllGameObjInactive())
    21.         {
    22.             print ("All are inactive? Do something!");
    23.         }
    24.         else
    25.         {
    26.             print ("one of the object is active");
    27.         }
    28.     }
    29.  
    30.     bool AreAllGameObjInactive()
    31.     {
    32.         foreach(GameObjScript gameObj in gameObjArray)
    33.         {
    34.             if(gameObj.gameObject.activeInHierarchy)
    35.             {
    36.                 return false;
    37.             }
    38.         }
    39.         return true;
    40.     }
    41. }

    Attach this to the GameObject that will be checked if active/inactive
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameObjScript : MonoBehaviour
    5. {
    6.  
    7.     CheckIt parentScript;
    8.  
    9.     void Awake()
    10.     {
    11.         parentScript = transform.GetComponentInParent<CheckIt>();
    12.     }
    13.  
    14.     void OnDisable()
    15.     {
    16.         parentScript.CheckObjStatus();
    17.     }
    18. }

    You can quickly test this by:
    1. Putting an empty gameObject on the scene. Attach the first script to it.
    2. Adding X amount of cubes inside the empty gameObject and attach the second script to them.
     
    Last edited: Oct 31, 2014
    rfuhrer likes this.
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Oh brilliant. Thanks for the great explanation. Really appreciate it :D