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?!

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.