- Home /
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?!
@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."
@salmjak then what would be the best way to do it?
@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.
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.
Thanks man, that works. And thanks @salmjak for that explanation.
Your answer
Follow this Question
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