Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 PictonicStudio · Mar 12, 2016 at 09:10 PM · c#childrenonguiforeacheditor window

What is the best way to grab and move the immediate children of a GameObject.

This isn't at runtime, this is in the OnGui, extending from EditorWindow

The closest thing I can get to work is

 string NodeGroup = "Example";
 Transform[] Children = GameObject.Find(NodeGroup).GetComponentsInChildren<Transform>();
 foreach (Transform Child in Children)
 {
        if (Child.GetSiblingIndex() == 1)
        Child.SetParent(NewParent.transform);
 }

Without if (Child.GetSiblingIndex() == 1), it moves everything, but the immediate children do not retain their children. This moves all of the children but one, for god knows what reason.

Now I don't know if it's a bug, but for example if I were to use

 foreach (Transform Child in GameObject.Find(NodeGroup).transform)
 {
         Child.SetParent(NewParent.transform);
 }


For some reason it only moves half of the chidren.

I guess I want to know if there is a better way, or if the way I am doing it is bugged. Like why is the second block of code moving only half of the children?!

Comment
Add comment · Show 3
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 Salmjak · Mar 12, 2016 at 09:38 PM 1
Share

@PictonicStudio In your second example I believe you are editing the array while iterating over it. It only goes through half of them, because when you have removed half of them from the list (by SetParent()) it has reached the end of the list. The list if shrinking while the index value is increasing.

From the docs: "The foreach statement is used to iterate through the collection to get the information that you want, but can not be used to add or remove items from the source collection to avoid unpredictable side effects."

avatar image PictonicStudio · Mar 12, 2016 at 09:48 PM 0
Share

@salmjak then what would be the best way to do it?

avatar image Salmjak PictonicStudio · Mar 12, 2016 at 09:56 PM 1
Share

@PictonicStudio Your first example probably :) Save all the gameobjects in a separate array and loop over the array (the array won't be changed/modified that way when you use SetParent).

I have no clue why the "grandchildren" won't move though.

1 Reply

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

Answer by Bunny83 · Mar 13, 2016 at 12:34 AM

GetComponentsInChildren return all components on the given gameobject or on any child object. So you don't get the Transform components of the direct children but all Transforms including the parent. That method does the same as GetComponents but in addition it also includes all the children as well. In the case of Transform this might cause trouble since every GameObject has a Transform.

As @Salmjak said in your second example you use Unity's internal IEnumerable to iterate through the immediate childs. However since you change the parent of the objects you're iteration over the internal array is messed up while you iterate through it.

The solution in this case is to store the objects in a temporary list or array before you manipulate the objects. You can use Linq to create that list / array.

 // at the top
 using System.Linq;
 
 // in your method
 var objs = GameObject.Find(NodeGroup).transform.Cast<Transform>().ToArray();
 foreach(var c in objs)
 {
     // [...]
 }

Note: The example is written from scratch so it could have some errors since i can't test at the moment.

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 PictonicStudio · Mar 13, 2016 at 02:03 AM 0
Share

Thanks man, that works. And thanks @salmjak for that explanation.

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

119 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

Related Questions

Switch for children not working 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Question on Using a Foreach Loop on Nested Children 1 Answer

Get Children of Child and Deactivate/Activate on Demand 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