Hi everyone, This little piece of code has a bug.
If we set the parent of a transform in a foreach loop of the parent, the next child is not enumerated.
I know removing an item while enumerating is not generally a good idea. But Unity don't even throw a warning about that, either in the doc or at runtime. On the other hand, C# or Mono throws an exception when we do something like that.
And I just spent an afternoon figuring why some object was correctly suppressed when some other doesn't while both of them pass the condition :-(.
asked Mar 11 '11 at 04:46 PM
A simple example depicting the suggestion put by Jessy.
Extra reading that is slowly but steadily going off topic ahead:
You can make use of predicates to allow to unparent a child following an arbitrary test:
And use it as such:
And you can even make it a bit more reusable if you also provide an action so you can do other things that just unparenting them:
Then you could do:
Like you say, you're not supposed to change the object you are enumerating (in this case 'transform').
This should work:
You could use some fancy LINQ stuff if you want for the 'find' bit.
answered Mar 11 '11 at 05:08 PM