|
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. 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?!?!?!??!!?!?!
(comments are locked)
|
|
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 NOTE: the less-hackish way is, instead of the Dummy, create
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)
|
|
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#) Javascript Unity has overloaded(?) I'm assuming they didn't just provide a variable like
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)
|
