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

[Solved] Looping through every child of child...

Discussion in 'Scripting' started by Iron-Warrior, Mar 22, 2010.

  1. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Hey there, I know that it's pretty easy to ignore collisions between two colliders with Physics.IgnoreCollision, but how can I ignore collision throughout an entire gameObject? I have a turret shooting a laser, with the turret's actual gun component instantiated the laser. However, the laser collider itself is larger enough to collide with the chassis of the turret, and needs that collider to be ignored. I know you can get the root of a transform with transform.root, but once I have it is it possible to loop through every child of that transform, then loop through those children's children and so on to IgnoreCollision with every collider in a gameobject?

    If there's an easier way this would be great, since it's probably something that will crop up now and again...thanks for any help!
     
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,183
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Does that loop through the children of the child game objects as well? Because my problem isn't being able to loop through the children and find the colliders, I can just get the root and do

    Code (csharp):
    1.             for (var child : Transform in theRoot)
    2.             {
    3.                 if (child.collider)
    4.                 {
    5.                     Physics.IgnoreCollision(laser.collider, child.collider);
    6.                 }      
    7.             }
    This unfortunatly only loops through the children of theRoot, rather than the children and the children's children...and so on...

    Anyways, I figured out that I could use a recursive function to solve the problem, seems to be working fine, but if there's a better solution I'd love to here it!

    Code (csharp):
    1. function DisableAllColliders(root : Transform, theCollider : Collider)
    2. {
    3.     for (var child : Transform in root)
    4.     {
    5.         if (child.collider)
    6.         {
    7.             Physics.IgnoreCollision(theCollider, child.collider);
    8.             Debug.Log(child.name);
    9.         }
    10.         if (child.childCount > 0)
    11.         {
    12.             DisableAllColliders(child, theCollider);
    13.         }
    14.     }
    15. }
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,183
    Well the docs say:
    The only way to really be sure is to check yourself!

    I have used it before but I can't recall if it will traverse children of children or just children (I think it will traverse all though, which is why I recommended it to you).
     
  5. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Just checked; it DOES loop through all of them. Thanks very much!
     
  6. noio

    noio

    Joined:
    Dec 17, 2013
    Posts:
    224
    If you need to loop through the entire hierarchy you could use nested IEnumerators:

    (this is the "pre-order" implementation, just move the "return root" around to get post-order or in-order traversal.

    Code (CSharp):
    1.     IEnumerable<GameObject> TraverseChildren(GameObject root)
    2.     {
    3.         yield return root;
    4.         foreach (Transform child in root.transform)
    5.         {
    6.             foreach (GameObject node in TraverseChildren(child.gameObject))
    7.             {
    8.                 yield return node;
    9.             }
    10.         }
    11.     }
     
    gareth_untether likes this.
  7. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    Those posts were from 2010...
     
  8. Twainstar00

    Twainstar00

    Joined:
    Mar 7, 2015
    Posts:
    46
    It's a good thing he posted an updated one.. I needed an answer too.