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 Jul 31, 2018 at 10:42 AM by pako for the following reason:

The question is answered, right answer was accepted

avatar image
4
Question by boddole · Mar 29, 2014 at 07:19 AM · c#getcomponentgetcomponentinchildren

GetComponentInChildren Returning a Component in Parent

I'm having a problem where GetComponentInChild actually is finding the component in the parent.

My hierarchy is more/less like so (and the script I'm testing this with is below): Parent (with UISprite) -child 1 (no UISprite component) -child 2 (with UISprite)

     void Awake ()
     {
         uiSprite = gameObject.GetComponentInChildren<UISprite>();
         uiSprite.name = "should be child";
     }

I am wondering if I am using this method incorrectly, or if this is the intended behavior? Any advice is appreciated

Comment
Add comment · Show 6
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 koray1396 · Mar 29, 2014 at 08:56 AM 1
Share

GetComponentInChildren returns one component, and it can detect the component of the parent as well as the child object. You can use "GetComponentsInChildren" to get an array and check if the component is attached to the parent object.

avatar image koray1396 · Mar 29, 2014 at 08:58 AM 1
Share

In addition scripting reference has already mentioned this.

GetComponent Returns the component of Type type if the game object has one attached, null if it doesn't. You can access both builtin components or scripts with this function.

GetComponentInChildren Returns the component of Type type in the GameObject or any of its children using depth first search.

GetComponents Returns all components of Type type in the GameObject.

GetComponentsInChildren Returns all components of Type type in the GameObject or any of its children.

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html

avatar image eaglemaster7 · Mar 29, 2014 at 09:03 AM 0
Share

I am not sure, I usually have exact isuue to you, so to make sure usually I type strictly like this:

THIS.gameObject.blablabla ins$$anonymous$$d of gameobject.blabla

sometimes that made different result, I make assumption that probably gameObject pointed to top hierarchy first,but sometimes fine as expected.

avatar image boddole · Mar 29, 2014 at 06:26 PM 0
Share

@ koray1396: you and pako are correct, using an array is a good solution for this, thank you both for the suggestions. I'm dissapointed in myself for not looking at the script reference but I think I just thought, "well if it says in child, why would it look in the parent?" But I should always check it anyway.

avatar image Artemis_Toh · Jan 06, 2016 at 06:44 AM 0
Share

@boddole Should this be reported to unity as a bug?

Show more comments

4 Replies

  • Sort: 
avatar image
16
Best Answer

Answer by pako · Mar 29, 2014 at 10:45 AM

Since GetComponentInChildren() searches the parent as well, and returns only the first component it finds, it will return the parent's component, if the parent has a UISprite attached. Therefore, the solution is to use GetComponentsInChildren() to get an array of all UISprite components found in parent and children, and then iterate through the array to find the one you want:

 void Awake ()
 {
 
     UISprite[] uiSprites = gameObject.GetComponentsInChildren<UISprite>();
         
       foreach(UISprite uiSprite in uiSprites)
       {
           if(uiSprite.gameObject.transform.parent != null)
           {
               uiSprite.gameObject.name = "should be child"; //this gameObject is a child, because its transform.parent is not null
           }
       }
 }
Comment
Add comment · Show 7 · 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 boddole · Mar 29, 2014 at 06:27 PM 0
Share

@ pako: you and koray1396 are correct, using an array is a good solution for this, thank you both for the suggestions. I'm dissapointed in myself for not looking at the script reference but I think I just thought, "well if it says in child, why would it look in the parent?" But I should always check it anyway.

avatar image tkamruzzaman · Oct 01, 2015 at 11:41 AM 0
Share

Thank you!

avatar image Artemis_Toh · Jan 06, 2016 at 09:33 AM 1
Share

@boddole I'm not sure why unity wanted GetComponentInChild() to start searching from it self(the gameObject that calls it), ins$$anonymous$$d of starting from the child. Should this be reported as a bug?

avatar image sonolil · Jan 23, 2016 at 10:29 AM 0
Share

Same sentiment. Why would unity want to do this in any way?

avatar image horoxix · Aug 10, 2018 at 10:37 PM 1
Share

Thank you so much for this!

Show more comments
avatar image
14

Answer by andrealaiena · Jul 14, 2014 at 11:50 AM

Simpler way I found:

 void Awake ()
     {
         uiSprite = gameObject.GetComponentsInChildren<UISprite>()[1]; //it "jumps" the parent and gives you the child component :D
 
         uiSprite.name = "should be child";
     }
Comment
Add comment · Show 3 · 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 pako · Jul 14, 2014 at 12:31 PM 1
Share

This would be a good way to directly access a child, only in the case that you know that a child is available and that the parent has the same component attached. Otherwise we'd get a null reference exception. It is applicable in this specific situation, but I think the statement "should be child" requires a more general answer. i.e. "should be child" is not true if there's no parent, and "should be child" is not true if the parent does not have the same component attached.

avatar image Cameron_SM · Jul 31, 2018 at 02:02 AM 1
Share

This is not a safe solution and I would recomment against this solution for the following reasons.

  1. It will fail and produce unpredictable results if and when the parent does not have a compontnent of type .

  2. It will throw an exception if there currently are no children with type .

In general, code that makes assumptions about state is bad and difficult to debug because the assumptions might not be obvious in the future or to other developers.

Ins$$anonymous$$d, make an extension method or explicitly asset the assumptions you're making:

 Assert.isNotNull(gameObject.GetComponent<UISprite>()) // parent has a UI Sprite
 Assert.IsTrue(gameObject.GetComponentsInChildren<UISprite>().Count > 1); // There are more than 1 objects returned from the call to GetComponents.
avatar image pako · Jul 31, 2018 at 10:34 AM 0
Share

Seeing that a post is still "alive" after 4 years shows how valuable this forum is.

So, after @Cameron860 gave some important points in his comments, I revisited this post, and noticed that @DottoProto 's answer produces a null reference as is: GetComponentsInChildren<UISprite>()[1] returns the first child, which does NOT have a UISprite component per the OP's description:

Parent (with UISprite) -child 1 (no UISprite component) -child 2 (with UISprite)

So, even in this particular case, this answer is wrong!

avatar image
4

Answer by Bomanden · Sep 26, 2016 at 10:07 AM

was just searching for the same .

with linq - if you dont want to include the parentsComponent.

 using System.Linq;
 var childrenList = this.GetComponentsInChildren<RectTransform>(true).Where(x => x.gameObject.transform.parent != transform.parent).ToList();


or even better , use neuecc's exelente linq to GameObject.

https://github.com/neuecc/LINQ-to-GameObject-for-Unity

 using Unity.Linq; //https://github.com/neuecc/LINQ-to-GameObject-for-Unity
 
 var Markers = GameObject.Find("Container").Children().OfComponent<Transform>().ToList();




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 ben-rasooli · Feb 02, 2016 at 09:52 AM

You might also consider this:

 uiSprite = transform.Find("Child Name").GetComponent<UISprite>();
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

Follow this Question

Answers Answers and Comments

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

Related Questions

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

transform.root and empty root game objects don't play nice 1 Answer

How to get a variable value from another script(C#)? 1 Answer

Distribute terrain in zones 3 Answers

insert script question 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