Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Grimmy · Aug 23, 2010 at 05:55 PM · childloopchildren

How do I loop through children and then their children until there are no more children?

I want to be as neat as possible so currently I am passing a transform to the function as follows..

function CalculateSteps(transformToCheck:Transform) {

 var child:Transform;

 for(var child:Transform in transformToCheck)
 {

     var childsToy:Toy;
     childsToy=child.gameObject.GetComponent(Toy);

     if(childsToy==theCorrectToy)
     {
         print("This is the one. you can stop looking.");
         return;
     }
 }

 //if it gets to here and it still hasnt found anything do it again using the child as the transform
 scannedThroughSetOfChildren+=1;
 if(child)
 {CalculateSteps (child);}//do this function again but with a child transform argument....

}

scannedThroughSetOfChildren only ever maxes at 2 scans..I want it to scan infinitely through all the parent's children, grand children, great children etc...until 'theCorrectToy' is found..

Basically i want to find out how many 'generations' ago a child had a specific toy.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by skovacs1 · Aug 24, 2010 at 06:20 AM

Searching is a fairly well-documented topic in computer science in general. You should try searching (answers, google, forums, etc.) before posting questions.

You aren't clear if you want to find the most recent generation to have the toy or if it even mattered.

Here are two simple approaches (both with breadth-first and depth-first searches):

I return the owner of the toy and its generation. You can change the return statements as you like if you only wanted the generation.

Recursive

Note that there may be a limit on the number of recursions allowed which may be why you are experiencing the problem that you are.

DFS

function findToy(parent : Transform, target : Toy) : Array { return findChildToy(parent, target, 0); }

private function findChildToy(parent : Transform, target : Toy, generation : int) : Array { if(parent.GetComponent(Toy) == target) return new Array(parent, generation); generation++; for(var child : Transform in parent) { var result = findToy(child, target, generation); if(result != null) return result; } return; }

BFS

function findToy(parent : Transform, target : Toy) : Array { var queue : Array = new Array(); queue.push(parent); return findChildToy(queue, target, 0); }

private function findChildToy(queue : Array, target : Toy, generation : int) : Array { var parents : int = queue.length; for(var i : int = 0; i < parents; i++) { var parent : Transform = queue.shift(); if(parent.GetComponent(Toy) == target) return new Array(parent, generation); for(var child : Transform in parent) queue.push(child); } if (queue.length == 0) return; return findChildToy(queue, target, generation++); }

Non-recursive

DFS

function findToy(parent : Transform, target : Toy) : Array
{
    var stack : Array = new Array();
    stack.push(new Array(parent, 0));
    while (stack.length != 0)
    {
        var parent : Array = stack.pop();
        if(parent[0].GetComponent(Toy) == target)
            return parent;
        parent[1]++;
        for(var child : Transform in parent[0])
            stack.push(new Array(child, parent[1]));
    }
}

BFS

function findToy(parent : Transform, target : Toy) : Array
{
    var queue : Array = new Array();
    queue.push(new Array(parent, 0));
    while (queue.length != 0)
    {
        var parent : Array = queue.shift();
        if(parent[0].GetComponent(Toy) == target)
            return parent;
        parent[1]++;
        for(var child : Transform in parent[0])
            queue.push(new Array(child, parent[1]));
    }
}
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cregox · Dec 03, 2010 at 07:16 PM 0
Share

Now I wonder how would that goes with C#... :P

avatar image skovacs1 · Dec 03, 2010 at 07:25 PM 0
Share

About the same. Reference http://answers.unity3d.com/questions/5507/what-are-the-syntax-differences-in-c-and-javascript for the syntax differences and you could use System.Collections.Generic.Queue and System.Collections.Generic.Stack for stack and queue. Ins$$anonymous$$d of passing item arrays, you could use System.Collections.Generic.$$anonymous$$eyValuePair.

avatar image
2

Answer by AliAzin · Aug 23, 2010 at 07:19 PM

you can use "GetComponentsInChildren()" instead of GetComponent();

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image yoyo · Jan 06, 2011 at 11:57 PM 0
Share

And then iterate back up through transform.parent to deter$$anonymous$$e ancestry.

avatar image
0

Answer by Andrew 10 · Jan 14, 2011 at 03:13 PM

static function FindTransform(parent : Transform, name : String) : Transform
{
    if (parent.name == name) return parent;
    var transforms = parent.GetComponentsInChildren(Transform);
    for (var t : Transform in transforms)
    {
        if (t.name == name) return t;
    }
    return null;
}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Change material of all the children 2 Answers

Loop to find a child in every children's child? 1 Answer

How do you detect which child collider was hit? 6 Answers

Values of a game object's children change when not supposed to 0 Answers

VR Rigidbody not working with child colliders 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges