I’ve a foreach loop for all transforms of a gameobject; something I used many times already. But this time the loop wont run completely, stopping after one cycle.
Here’s the script:
Explained: foreach transform in the gameobject “CargoSpace” should following things be made: Add the (transform)object to a list (that works). Remove it from another list (that works too). Change parent (guess what: it works). But the foreach wont work:
Correctly (as I know) should happen:
A: 1 times (pressing the button once)
B: 2 times (once for each of the two objects in transform)
C: 2 times (once for each of the two objects in transform)
And yeah, there are two objects in transform.
How can I make it work for all of the objects? I’ll try it with a for loop, but thats a detour…
You change the parent of the elements you iterate over. That means you mess up the enumerator since the “collection” of objects is changed from within the loop. You effectively will skip every second element.
Have a look at this. The pipe | should represent the internally used index:
Only explanation would be that it can only find 1 transform with the name CargoSpace on the baseItem. Why don’t you iterate the list you’re removing the item from and compare it.