• 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 Oct 22, 2013 at 09:49 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by d112570 · Oct 22, 2013 at 08:10 PM · parentchildname

Get Parent name of child

I found this child, and I need to know who their parents are, here's the script.

 Transform[] allChildren = GetComponentsInChildren<Transform>();
 foreach (Transform child in allChildren){

    if (child.name == "4" && child.parent.name == "A") { // Here I need help with
       var script1 = child.transform.gameObject.GetComponent<ScriptA>();
       if (script1 == null)  child.transform.gameObject.AddComponent("ScriptA");
 }}

This is what I need help with child.parent.name. I tried different versions, adding transform, adding gameObject. I get Null Reference error.

Example: There are 3 parents with the name A, B, and C. Each have 9 children named 1 to 9. If child 1 is parented to A then add Script A, if child 5 is parented to C then add Script C etc. This will only be activated once, since its in the void start area.

Comment
Add comment · Show 8
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 Gjallanhorn · Oct 22, 2013 at 08:19 PM 0
Share

this script is attached to parent?

avatar image meat5000 ♦ · Oct 22, 2013 at 08:20 PM 0
Share

transform.name

avatar image d112570 · Oct 22, 2013 at 08:21 PM 0
Share

Indeed it is.

avatar image robertbu · Oct 22, 2013 at 08:24 PM 1
Share

I'm not sure what is going on, but a couple of things to note. child.parent.name should work as long as child.parent is not null. GetComponentsInChildren() will also return the component of the specified type in the game object used as the root. In this case, the game object this script is attached to. I'm guessing this script is on the parent. Put some Debug.Log() statements in your code:

 Debug.Log(child.name+", "+child.parent);

If you see a null for 'child.parent', you know you've spotted a potential problem.

avatar image d112570 · Oct 22, 2013 at 08:24 PM 0
Share

to "meat5000" transform.name get the child's name, anyways you don't need transform, child.name does fine, to find its name. I need parent not child.

avatar image Gjallanhorn · Oct 22, 2013 at 08:30 PM 0
Share
 if(transform.name == "A")
 {
     var script1 = child.transform.gameObject.GetComponent<ScriptA>();
     if (script1 == null)
         child.transform.gameObject.AddComponent("ScriptA");
 }
avatar image d112570 · Oct 22, 2013 at 08:31 PM 0
Share

Ok, thx Robert as always. I had to add .name otherwise it would't work as a string.

 if (child.name == "1" && child.parent.name == "Planets") print(child.name+", "+child.parent);

You should of posted it as an answer.

avatar image meat5000 ♦ · Oct 22, 2013 at 08:47 PM 0
Share

to "meat5000" transform.name get the child's name, anyways you don't need transform, child.name does fine, to find its name. I need parent not child.

I mentioned transform.name as it looked like you were using a FindInChildren without a reference object/transform meaning it would find all children of the object the script is attached to. transform.name gives the name of the transform object the script is attached to.

$$anonymous$$aybe I misunderstood what you were really asking :)

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by rutter · Oct 22, 2013 at 08:31 PM

transform.parent will give you the parent transform, or null if there is no parent (ie: it's at the top of the hierarchy).

When you call GetComponentsInChildren(), that will also include the Transform of the script's own GameObject. If that GameObject is at the top of the hierarchy, it won't have a parent and you'll get a NullReferenceException.

So, with that in mind: what do you want to do with the top object? Do you want to ignore it, or should it get a ScriptA attached as well?

To ignore it:

 if (child.name == "4" && (child.parent != null && child.parent.name == "A"))

To include it:

 if (child.name == "4" && (child.parent == null || child.parent.name == "A"))

In both of the above cases, a parent equal to null will "short-circuit" the logic evaluation, so that we never try to access its parent.

One last note, I notice you're trying to access child.transform.gameObject. If child is already a Transform, you probably don't need that extra reference.

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 d112570 · Oct 22, 2013 at 08:38 PM 0
Share

thx, robert already solved it, samed answer as yours. I actually just made child.parent.name up and didn't even try it, I feel real stupid now, I had added transform and game object, in front, then back, then middle anyway I can think of, while writing this on questions, I made it short and simple and had no idea this would work.

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

17 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

Related Questions

Make a simple tree 1 Answer

How to check parent value instead name value(more details in post) 3 Answers

Find gameobject with name and if child of Player 1 Answer

[C#] How do I instantiate multiple objects at the same time? 1 Answer

find ALL children of a parent 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