using foreach transform child, unity gets stuck

this script locks up or infinite loops when i run the game. the script is attached to a gameobject which has about 30 empty gameobjects as children. the idea is to instantiate the c1 object at each child’s position.

public GameObject c1;

void Start () 
{
    foreach (Transform child in transform)
    {
        GameObject go =
          (GameObject)Instantiate(c1
          ,child.transform.position
          ,Quaternion.identity);
        go.transform.parent = transform;
    }
}

HOWEVER, the following runs fine when i remove the foreach child junk…it also works if i remove the nested commands and leave just the foreach loop…so the loop works on its own…and the instantiating of gameobjects works on its own…but when i combine them like above unity freezes…what!!!

public GameObject c1;

void Start () 
{
    GameObject go =
       (GameObject)Instantiate(c1
       ,transform.position
       ,Quaternion.identity);
    go.transform.parent = transform;
 
}

it could be cuz im adding gameobjects to the parent while im still looping through the children… so as its looping its creating more children to have to loop through…creating an infinite loop…i’ll post back when i can…when i realize whatever obvious mistake im making…i realized this mistake as soon as i stepped outside…but thats how it goes

in response to doireth…all the foreach transform examples ive seen in Unity work this way…but i can give it another look…this script is a component of the object im accessing the children of…so by saying foreach(Transform child in transform) its saying each childs transform which is parented to the transform of the main object

foreach works on data structures. Transform (that I know of) is not in any way considered as such.

Also, “child” in the foreach declaration is a variable that you have created and doesn’t pertain directly anything in the transform.

Try: (C#)

Transform[] Children = GetComponentsInChildren<Transform>();
foreach (Transform child in Children) {
    // whatever
}

Javascript

var Children = gameObject.GetComponentsInChildren(Transform);
for (var child : Transform in Children) {
    // whatever
}