- Home /
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;
}
Answer by mononull · Jul 20, 2012 at 09:00 PM
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
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, ins$$anonymous$$d of the Dummy, create LinkedList New$$anonymous$$ids;
and use New$$anonymous$$ids.AddLast(go.transform);
. Then the same foreach
to parent them. But not unless you've used LinkLists before.
Thanks mononull for the info. Shame such things are not documented.
Answer by Doireth · Jul 20, 2012 at 08:23 PM
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
}
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 assu$$anonymous$$g 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.
Thanks Owen I didn't know that. A much more succinct method.
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 ;)
Your answer
Follow this Question
Related Questions
Unparenting a transform in a foreach loop in the parent transform 3 Answers
Make a simple tree 1 Answer
Separate child rotation 1 Answer
Assign parent object to target 2 Answers