# ForeEachLoop Crashing the game :( I don't understand why

**URL:** <https://forums.unrealengine.com/t/foreeachloop-crashing-the-game-i-dont-understand-why/392945>\
**Category:** Platform & Builds\
**Tags:** foreachloop, while-loop, unreal-engine, crash, question\
**Created:** [May 7, 2017, 6:37am UTC](https://forums.unrealengine.com/t/foreeachloop-crashing-the-game-i-dont-understand-why/392945 "2017-05-07T06:37:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheNomadNamedY](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheNomadNamedY](https://forums.unrealengine.com/u/TheNomadNamedY)\
**Post date:** [May 7, 2017, 6:37am UTC](https://forums.unrealengine.com/t/foreeachloop-crashing-the-game-i-dont-understand-why/392945/1 "2017-05-07T06:37:39Z")

</div>

I have a grid of tiles and an array of them that is made using the “get all of class” node. BEcause of this, the array is random. I’m trying to sort the array by name (into a new array that is) but i’m getting a callstack crash from this fore each loop that i don’t really know the answer for. can someone tellme why i’m getting this error?

 ![137081-ss1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/f/0/6f03ba4461a54cb6b41fe9a35a987d44702bd78d.png)

this is the second path in a branch in a “for each” loop. the first is for when the sort number is = to 0 (the first tile spawns as just “FloorTile” without the zero)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_aa6725fb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_aa6725fb](https://forums.unrealengine.com/u/anonymous_user_aa6725fb)\
**Post date:** [May 7, 2017, 7:07am UTC](https://forums.unrealengine.com/t/foreeachloop-crashing-the-game-i-dont-understand-why/392945/2 "2017-05-07T07:07:51Z")

</div>

You are removing objects from the array while you are iterating it with ForEach. I’m pretty sure that is the source of your crash. Manipulating the array in certain ways while going through it will invalidate the iterator and can cause a crash. Instead what you can do is have another empty array called “ToBeRemoved” and add your tiles that you have already carried over instead of removing them from TemporaryTiles outright. Then you can remove all the elements of ToBeRemoved from TemporaryTiles after your ForEach is finished, before you increase SortNumber.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![TheNomadNamedY](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheNomadNamedY](https://forums.unrealengine.com/u/TheNomadNamedY)\
**Post date:** [May 7, 2017, 7:28am UTC](https://forums.unrealengine.com/t/foreeachloop-crashing-the-game-i-dont-understand-why/392945/3 "2017-05-07T07:28:47Z")

</div>

Thank you 🙂 it’s almost working perfectly now. the only problem is that it stops at tile 32 and doesn’t continue with the other 59

 ![137084-ss2.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/a/6/ca61241085a74cd7c3b84e8b9379cdcaca5c0bc5.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_aa6725fb](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_aa6725fb](https://forums.unrealengine.com/u/anonymous_user_aa6725fb)\
**Post date:** [May 7, 2017, 7:44am UTC](https://forums.unrealengine.com/t/foreeachloop-crashing-the-game-i-dont-understand-why/392945/4 "2017-05-07T07:44:33Z")

</div>

I think it’s because you’re completely clearing TemporaryTiles after the first iteration. You could do it like this:

 ![137085-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/5/6/656305f8855fdebb51323c5e9618728eeaded44c.png)

---

<div class="post-metadata">

**Author:** ![TheNomadNamedY](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@TheNomadNamedY](https://forums.unrealengine.com/u/TheNomadNamedY)\
**Post date:** [May 7, 2017, 7:48am UTC](https://forums.unrealengine.com/t/foreeachloop-crashing-the-game-i-dont-understand-why/392945/5 "2017-05-07T07:48:03Z")

</div>

thanks x2 lol i’d be beeting my head against my laptop if it wasn’t for you 😃
