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 /
avatar image
1
Question by donutusable · Mar 19 at 05:20 PM · childforeach

How to go up or down to assign a child?

I want to find a child apart of a transform and then go one above or below it. I hope that explains it, its kind of difficult to explain but here's the code:

             if (Input.GetKeyDown(KeyCode.W))
             {
                 foreach (Transform child in hook)
                 {
                     if (child.name == currentSegment.gameObject.name)
                     {
                         //what I want is getting the child I found and then going to the one above it
                         currentSegment = child++;
                     }
                 }
             }
             else if (Input.GetKeyDown(KeyCode.S))
             {
                 foreach (Transform child in hook)
                 {
                     if (child.name == currentSegment.gameObject.name)
                     {
                         currentSegment = child--;
                     }
                 }
             }
Comment
Add comment · Show 5
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 Narc0t1CYM · Mar 19 at 05:38 PM 0
Share

What is hook in this context? Have you tried doing something like child.parent ? You can also chain **.parent**s like so child.parent.parent because it will always point to a transform - if it exists of course

avatar image donutusable Narc0t1CYM · Mar 19 at 05:42 PM 0
Share

Hook is the transform that is the parent to the children Im trying to access. The currentSegment is a child that has been assign already but what Im trying to do is go from that one to the above and below it on the children of the hook in the hierarchy.

Edit: Im really sorry if this doesn't make sense Im struggling to explain it

avatar image Narc0t1CYM donutusable · Mar 19 at 06:07 PM 0
Share

To go above, you can use transform.parent for any transform. To check for the children one level below, I'd do something like this.

Assuming all children have a Transform component attached to them - which they should by default - you could do a foreach cycle and then check if the current transform.parent is the parent you are looking for, like so:

 Transform parent = transform;
 
 foreach (Transform child in transform.GetComponentInChildren<Transform>()) 
 {
     if (child.parent == parent) {
         // It is the parent's child
     }
 }

If you want to go more deeper then you have to do it recursively, but you should be careful with that

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Narc0t1CYM · Mar 19 at 06:16 PM

To go above, you can use transform.parent for any transform. To check for the children one level below, I'd do something like this.

Assuming all children have a Transform component attached to them - which they should by default - you could do a foreach cycle and then check if the current transform.parent is the parent you are looking for, like so:

 Transform parent = transform;
  
 foreach (Transform child in transform.GetComponentInChildren<Transform>()) 
 {
     if (child.parent == parent) {
         // It is the parent's child
     }
 }


If you want to go more deeper then you have to do it recursively, but you should be careful with that.

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

133 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 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 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 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

Get Children of Child and Deactivate/Activate on Demand 1 Answer

using foreach transform child, unity gets stuck 2 Answers

Problem with finding compnent of immidate children 1 Answer

Destroy specific child object 1 Answer

transform.position = new Vector3 Bug or error 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