x


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;

}
more ▼

asked Jul 20 '12 at 08:07 PM

mononull gravatar image

mononull
38 1 5 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

more ▼

answered Jul 20 '12 at 09:00 PM

mononull gravatar image

mononull
38 1 5 7

That sounds about right -- you never run out of children, so run forever.

A hack might be to have a dummy empty, to hold them as you create, then reparent later. In the loop, say go.transform.parent=Dummy;. Then run foreach in Dummy and say go.transform.parent=transform;.

NOTE: the less-hackish way is, instead of the Dummy, create LinkedList NewKids; and use NewKids.AddLast(go.transform);. Then the same foreach to parent them. But not unless you've used LinkLists before.

Jul 21 '12 at 03:34 PM Owen Reynolds

Thanks mononull for the info. Shame such things are not documented.

Jul 21 '12 at 07:34 PM Doireth
(comments are locked)
10|3000 characters needed characters left

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
}
more ▼

answered Jul 20 '12 at 08:23 PM

Doireth gravatar image

Doireth
602 4 7

Unity has overloaded(?) foreach so that running it over your transform does go through your children. It's the "official" way to touch each child.

I'm assuming they didn't just provide a variable like Transform[] transform.children is that children are really stored in some odd way and they didn't want to scare anyone.

Jul 21 '12 at 03:24 PM Owen Reynolds

Thanks Owen I didn't know that. A much more succinct method.

Jul 21 '12 at 07:33 PM Doireth

Unity has not overloaded foreach ;) The Transform class implements the IEnumerable interface. That means Transform provides a GetEnumerator function which returns an IEnumerator which can be used to iterate through the direct children of thie transform.

See the foreach documentation ;)

ps: the documentation page of Transform directly shows an example with a foreach loop ;)

Aug 07 '12 at 10:18 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1279
x422
x410
x63

asked: Jul 20 '12 at 08:07 PM

Seen: 1002 times

Last Updated: Aug 07 '12 at 10:18 PM