Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Check if all gameobjects is trigger exit

Discussion in 'Scripting' started by MrJohny, Oct 3, 2015.

  1. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    Hello
    I have little problem. I have gameobjects in parent gameobjects.
    How to detect if ALL childs is trigger exit?
    (I tried set bool to the every child when is trigger exit and than chceck in parent if all childs bool is true... It is nonsense of course)
    Sorry for my english, is not my native language.
     
  2. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Code (CSharp):
    1. //This array will hold the children
    2. public GameObject[] childrenObj;
    3.  
    4. void Start()
    5. {
    6.  
    7. //This will get all the children of the object you want
    8. int children = transform.childCount;
    9. childrenObj = new GameObject[children];
    10. for (int i = 0; i < children; i++)
    11.        childrenObj[i] = transform.GetChild[i];
    12.  
    13. }
    14.  
    15. void OnTriggerExit(Collider other)
    16. {
    17.       //Now we check if each object is colliding with "Something" tag.
    18.       if(other.tag == "Something")
    19.       {
    20.            for(int i = 0; i < children; i++)
    21.                  print(childrenObj[i].name);
    22.       }
    23. }
    24.  
     
  3. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    ummm... I had something similar but, parent of gameobjects dont have collider, i want check if childs triger exit from third gameobject... I forgot to mention that the parent is only "group" for childs...
     
  4. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    I know, the parent does not matter as long as it exists.
    What you want to do is:
    Code (CSharp):
    1. for(int i = 0; i < children; i++)
    2.      childrenObj[i].GetComponent<BoxCollider>().triggering = true;
    When you take the code I gave you in the first place, and let it archive the children, you can do whatever you want with them and access them from anywhere at all!

    As I said in my first code, I wrote:
    Where it says "if(other.tag == "Something")" is where we check what the other collider is called/tagged/anything you want to check. In there, you can write the name of the other 'third' object you're talking about, then you can just say:
    Code (CSharp):
    1. for(int i = 0; i < children; i++)
    2.      childrenObj[i].GetComponent<BoxCollider>().triggering = true;
    where it says "print(childrenObj.name);"
     
    Last edited: Oct 3, 2015
    MrJohny likes this.
  5. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    Ummm okay, that is good. I had it similiar, but i want destroy parent, ONLY if all childs are trigger exit. And this is problem, because if any child is trigger exit, bool set to true. I dont know how to check if ALL bool are true.
     
  6. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    Oh I see.
    You'll need a bool array in that case, like this:
    Code (CSharp):
    1. public static bool[] triggering;
    2. public static int amountNotTriggering;    //We'll use this one to increase the amount of bools true
    3.  
    4. void Start()
    5. {
    6. triggering = new bool[children];
    7. }
    And now just paste this into your OnTriggerExit():
    Code (CSharp):
    1. void OnTriggerExit(Collider other)
    2. {
    3.       //Now we check if each object is colliding with "Something" tag.
    4.       if(other.tag == "Something")
    5.       {
    6.            for(int i = 0; i < children; i++)
    7.            {
    8.                  triggering[i] = false;
    9.            }
    10.       }
    11. }
    After that, you can use Update() to check if all the bools are true!
    Code (CSharp):
    1. void Update()
    2. {
    3.     for(int i = 0; i < children; i++)
    4.     {
    5.             if(triggering[i] == false)
    6.               amountNotTriggering += 1;
    7.     }
    8. }
    And now finally, we can check to see if amountTriggering is equal to the amount of children, meaning all the bools are true:
    Code (CSharp):
    1. //Paste this into void Update()
    2. if(amountNotTriggering== children)
    3. {
    4.      print("Amount of objects not triggering anymore: " + amountNotTriggering);
    5.      Destroy(this.gameObject); //This will destroy the current transform as you wanted
    6. }
    Basically,
    we setup a new boolean with amount of elements equal to the amount of children. Then, we check for each child object if they're triggering or not, and if they're not, the bool specific for them gets set to false. And for each bool that is false, we increase the int amountNotTriggering by 1. At the end, we check if amountNotTriggering is equal to the amount of children there are, and if it is then we do something!
     
    Last edited: Oct 3, 2015
    MrJohny likes this.
  7. MrJohny

    MrJohny

    Joined:
    Feb 19, 2015
    Posts:
    73
    awesome, thanks
     
  8. Sykoo

    Sykoo

    Joined:
    Jul 25, 2014
    Posts:
    1,394
    You're welcome :)
    If you ever get stuck, don't hesitate to ask!