• 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
1
Question by Oninji · Dec 10, 2010 at 09:44 PM · gameobjectparentchildfind

Can I limit a .Find to the Parents and Child and not the Scene?

I would like my Script to search for an object in all parents/childs/Sibling and not the scene.

How would I achieve that?

Edit :

To be more precise, I'm looking for an alternative of a GameObject.Find but that would only search objects with a relation with the script (parents, child, siblings), thus it would not mistake and "Find" object in similar objects in the scene.

so "Ball 1" would not look for objects in "Ball 2" and would limit it's search to it's own parent "World".

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
3
Best Answer

Answer by skovacs1 · Dec 10, 2010 at 10:07 PM

"All parents/childs/sibling" is somewhat vague. Perhaps you could rephrase to be more descriptive. Where do you want to start from? Which direction do you want to go? How far do you want to go? When searching down, would you rather go as deep as you can first, searching from one side of the tree to the other or row by row?

Parenting forms a tree.

Searching only this GameObject's parents (going up the tree):

var foo : Transform;
var current : Transform = transform;
while(current.parent) {
    current = current.parent;
    if(current.name == "foo") {
        foo = current;
        break;
    }
}

Searching all immediate children (searching the next row of the tree can be achieved by Transform.Find:

var foo = transform.Find("foo");

To search from the highest parent (the top of the tree), you would need to get this root transform and do the above there like so:

var foo = transform.root.Find("foo");

This is more meaningful if you know the exact path which you would delimit with '/' and grandchildren will be searched.

To search only siblings, you would go up one parent and then check all of its children like so:

var foo : Transform;
if(transform.parent) foo = transform.parent.Find("foo");

It isn't as fast, but to search all children at every level, you would use Transform.GetComponentsInChildren like so:

var foo : Transform;
for(var child : Transform in transform.GetComponentsInChildren(Transform)) {
    if(child.name == "foo") {
        foo = child;
        break;
    }
}

Likewise, to search the entire tree with no knowledge of the relationships, you would do as above with the root:

var foo : Transform;
for(var child : Transform in transform.root.GetComponentsInChildren(Transform)) {
    if(child.name == "foo") {
        foo = child;
        break;
    }
}

Using GetComponentsInChildren uses Depth-First-Search. If you had wanted to do a Breadth-First-Search, you would have to write one like so:

var foo : Transform;
var queue : Array = new Array();
for(var child : Transform in transform) queue.push(child);
while(queue.length != 0) {
    var current = queue.shift();
    if(current.name == "foo") {
        foo = current;
        break;
    }
    for(var child : Transform in current) queue.push(child);
}  
Comment
Add comment · Show 5 · 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 Oninji · Dec 10, 2010 at 10:37 PM 0
Share

Like all my other attempts, it returns me Assets/Scripts/$$anonymous$$enu/lockScript.js(9,34): BCE0022: Cannot convert 'UnityEngine.Transform' to 'System.Type'.

avatar image Oninji · Dec 10, 2010 at 10:39 PM 0
Share

Never$$anonymous$$d, forgot a detail. Erf.

avatar image Oninji · Dec 10, 2010 at 10:42 PM 0
Share

The fifth one worked, toyed with it and could fetch the GameObject from it. Thanks.

avatar image yoyo · Mar 20, 2015 at 05:18 PM 0
Share

Wish there was a way to find all root objects in the scene. :-P

Given any transform, you can find a root object with transform.root -- so then if you could find all that root's siblings you'd be all set. Unfortunately you can't find siblings except by going through a parent, which for a root node doesn't exist. :-P :-P

avatar image fafase · Mar 20, 2015 at 05:31 PM 0
Share

@yoyo you can do:

 GameObject [] objs = GameObject.FindObjectsOfType<GameObject>();
 List<GameObject>list = new List<GameObject>();
 foreach(GameObject obj in objs){
    if(obj.transform.root == obj.transform){
        list.Add(obj);
    }
 }
 return list.ToArray();

This will return all active top objects in your scene.

avatar image
1

Answer by Thom Denick · Dec 10, 2010 at 10:06 PM

It's already built into the engine. You need to grab all the children from the parent by following along here:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentsInChildren.html

The code you will want to modify to check each object for a tag. (I'm assuming you want an alternative to FindGameObjectByTag

var transforms : Transform[];
transforms = gameObject.GetComponentsInChildren(Transform);
for (var transform : Transform in transforms) {
    if(transform.gameobject.tag == "target") {
      //Do what you like.
    }
}

Looking at your addendum, I think what you want to do is get the parent of the objects you want to search, then cycle through all children via the GetComponentsInChildren above until you get the GameObject you are looking for.

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
avatar image
0

Answer by Justin Warner · Dec 10, 2010 at 11:00 PM

See Accessing Other Game Objects in the Unity 4.1.2 documentation.

Check number 2 please.

Should be good =).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Make a simple tree 1 Answer

how do I get reference to a parent or a child? 2 Answers

"Center On Children" programmatically 1 Answer

How to destroy a parent object when child object is collided 1 Answer

Calling a function of another object's child? 1 Answer


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