Unity optimization tips & tricks: Foreach loops, part 2

Foreach loops, part 2 This is part 2 out of a 2-part series on foreach loops. Click here for part 1. Last time, we played around with foreach loops, and ended up deconstructing the loop into an iterative while loop. For fun and for profit- and for superior performance in the Unity engine! Today though, we’re going a little deeper. But first, let’s take a look a the code we ended with last week and see where we can improve: int iterationCount = 0; while(iterationCount < collection.Length) { var element = collection[iterationCount]; // the body of the foreach loop iterationCount ++; } First things first – I’m going to cache the length of the array (or the count of a Read more about Unity optimization tips & tricks: Foreach loops, part 2[…]