Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Sep 23, 2013 at 12:20 PM by Ranger-Ori for the following reason:

The question is answered, right answer was accepted

avatar image
2
Question by Ranger-Ori · Mar 17, 2013 at 11:34 AM · getcomponentparentgetcomponentinchildren

Get Components without parent?

Hello, quick question!

Sorry if that's too basic, I looked into the documentation and I couldn't find a function or method that returns the Components in a gameobject without the parent gameobject.

Thank you for your answers.

Comment
Add comment · Show 1
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 jeromeWork · Mar 23, 2016 at 03:22 PM 0
Share

This works too (sorry question closed so put it here)

 foreach (Transform tr in transform) Debug.Log(tr.name);

(from: http://forum.unity3d.com/threads/getcomponentsinchildren-not-parent-and-children.222009/#post-1480399)

2 Replies

  • Sort: 
avatar image
9
Best Answer

Answer by GuyTidhar · Mar 17, 2013 at 11:46 AM

You can do this:

 var comps : yourType[];
 comps : GetComponentsInChildren.<yourType>();
 
 for(var comp : yourType in comps)
 {
    // Is my ID (the parent) not the same as the component's gameObject
    if ( comp.gameObject.GetInstanceID() != GetInstanceID() )
    {
    }
 }

Or in C#:

 YourType[] comps = GetComponentsInChildren<yourType>();
 foreach (YourType comp in comps)
 {
        if ( comp.gameObject.GetInstanceID() != GetInstanceID() )
        {
        }
 }
Comment
Add comment · Show 6 · 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 Ranger-Ori · Mar 17, 2013 at 01:51 PM 0
Share

Thank you guy! it was really helpful.

avatar image GuyTidhar · Mar 17, 2013 at 01:52 PM 0
Share

Glad to have helped!

avatar image Xnerdz · May 12, 2014 at 04:38 PM 0
Share

I'm just wondering, is GetInstanceID faster than just comparing the objects? For exemple, would: if ( comp.gameObject != this.gameObject ) {...} be the same?

avatar image Sarkahn · Jul 26, 2015 at 01:40 PM 0
Share

@Xnerdz I was curious about this so I checked from the decompiled source, you can see here:

Comparing two UnityEngine.Objects essentially boils down to comparing their Instance IDs with a couple of extra checks. $$anonymous$$aybe a little slower but probably not worth worrying about.

avatar image Xnerdz · Jul 26, 2015 at 06:10 PM 0
Share

@Sarkahn You're probably right. Anyway, by the time I wrote that comment, I learned that premature optimization is the root of all evil.

Show more comments
avatar image
4

Answer by fafase · Mar 17, 2013 at 12:12 PM

Here is a script that allows you to get all child script except the root object which I assume is what you meant by parent object (the very top one)

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic; //Necessary!!!!
 
 public class NPCScript : MonoBehaviour {
     
     void Start(){
         HealthScript [] sc = GetCompNoRoot<HealthScript>(gameObject);
         foreach(HealthScript s in sc){
             s.health = 10;
         }
     }
     
     T[] GetCompNoRoot<T>(GameObject obj)where T:Component{
         List<T> tList = new List<T>();
         foreach (Transform child in obj.transform.root)
            {
                T[] scripts = child.GetComponentsInChildren<T>();    
             if(scripts != null) {
                 foreach(T sc in scripts)
                     tList.Add (sc);
             }
         }
         return tList.ToArray();
     }
 }

The method is GetCompNoRoot. It takes the script name as generic parameter and I added a game object parameter in case you wold store that method somewhere else like a Utility file.

All it does is create a list, get the root object of the parameter object and starts going down the hierarchy. In the end it returns an array of all scripts found.

Now if you meant no parent object as in no parent object simply gets this

 foreach (Transform child in obj.transform.root)

to

 foreach (Transform child in obj.transform)

Then if the script is on a child object in a hierarchy, it will ignore anything above but will go down also ignoring the object to which it is attached.

Note that this will work with any script or Component because of the constraint (where T: Component).

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 Ranger-Ori · Mar 17, 2013 at 01:52 PM 0
Share

Thank you for your answer! I'm pretty sure your answer is correct, but Guy's way is more practical to my problem.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GetComponentInChildren Returning a Component in Parent 4 Answers

Take array and waypoints from parent spawnner 2 Answers

GetComponent in parent question 3 Answers

How to get script from Object with (Clone)'s script? 2 Answers

How to call GetComponent by name? GetComponents? 2 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