In unity, in C#, I have the following code in Awake():
Awake()
{
Transform obj = InstantiateTarget();
//the bizarrely required and "working" loop
while (transform.childCount > 0)
{
//assign all current children to the replacement object
foreach (Transform t in transform)
{
t.parent = obj;
}
}
Destroy(gameObject);
}
Without the while loop, some random set of the transforms children are destroyed by that end Destroy
. With the while, it works exactly as expected (all transform's children become obj's children).
Also, surely with it, there should be an infinite loop (if nothing is changing in another thread, as implied by unity docs). This is in Unity 3.5.
Why does it only work with the while loop? And why doesn't unity get stuck in an infinite loop?
transform
\$\endgroup\$