Buggy child parenting/unparenting? How to avoid For-Loop wackiness?

Generally speaking, I've come across this problem several times - enough to realize that my procedural mind is single-track, but also that Unity behaves poorly in this circumstance.

It appears to be that when looping over the children of one parent object, the child objects cannot be modified without things going wonky.

For example, one cannot loop over the children of one object and set the parent of each to another object. The result is two parents with children assigned in some opaque fashion.

Is there a way to do achieve this without a For-Loop?

If you're looping over a transform's children and then changing their parent, you're changing the collection that you're looping through. It is like iterating through any array and deleting elements in the middle; you don't do that, unless you're, say, iterating using an index in reverse order.

So copy the collection first, or iterate by index backwards. Here's an example of copying first (C#):

public void AdoptChildren( Transform currentParent, Transform newParent )
{
    List<Transform> children = new List<Transform>();
    foreach( Transform child in currentParent )
        children.Add( child );

    foreach( Transform child in children )
        child.parent = newParent;
}

You can create an array of the children first and then perform the actions on the array, this should eliminate unwanted behavior.

I reported this bug on September 27th of last year, and it is still open. I haven't actually needed to do this again for awhile, so I haven't picked my own method, but I verified that what Morten Sommer of the Support Team offered is a successful workaround:

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class CopyPasteChildren : ScriptableObject
{

    static List<Transform> transformBuffer;

    [MenuItem("CONTEXT/Transform/Copy Children")]
    static void Copy(MenuCommand mCommand)
    {
        Transform parentTransform = (Transform)mCommand.context;

        if (parentTransform != null)
        {
            transformBuffer = new List<Transform>(parentTransform.GetComponentsInChildren<Transform>());
            transformBuffer.Remove(parentTransform.transform);
        }
    }

    [MenuItem("CONTEXT/Transform/Paste Children")]
    static void Paste(MenuCommand mCommand)
    {
        if (transformBuffer == null || transformBuffer.Count == 0)
            return;

        Transform parentTransform = (Transform)mCommand.context;

        if (parentTransform == null)
            return;

        foreach (Transform tr in transformBuffer)
        {
            tr.parent = parentTransform;
        }
    }
}

Here's the way I used to take care of it:

bool reparenting = true;
while (reparenting)
{
    if (oldParent.transform.childCount > 0)
        foreach (Transform child in oldParent.transform)
            child.parent = newParent.transform;
    else reparenting = false;
}