0:07
In this lecture, we'll see how we can selectively process
 the game objects in our game using a list and a for loop.
 And the title may have already told you that
 the game objects that we'll be processing are teddy bears.
 And by processing, I mean blow up.
 So, let's go see how we can do that.
 As usual, I'll start by showing you how this game works.
 So, it periodically spawns teddy bears.
 And if I press the left mouse button,
 the yellow teddy bears explode,
 right mouse button I explode all the green ones,
 and middle I explode the magenta ones.
 And I can just keep doing this,
 and keep doing this for as long as I want, and it works great.
 And I get to destroy teddy bears.
 Let's go look at the scripts that actually
 do the interesting processing for this problem.
 The less interesting script is the TeddyColor enumeration,
 but I'm going to want to compare colors,
 and I know there are only three,
 green, purple, and yellow.
 So I've implemented an enumeration for those colors.
 As you can see in the inspector for the main camera,
 we still have the Teddy Bear Spawner script attached to the main camera,
 but we've also attached this Blowing Up Teddies script,
 the game manager to the main camera as well.
 And this just shows that you can have multiple scripts attached to
 a GameObject to implement your desired behavior.
 The BlowingUpTeddies script is the script that does the heavy lifting.
 And we have a number of serialized fields in the class.
 So, we have a prefab for the explosion,
 and we have references to the yellow,
 green, and purple sprites,
 so that we can compare the sprite for
 a particular teddy bear to those sprites to determine that teddy bears color.
 We also will have a list of GameObjects that we
 retrieve when the player provides some mouse button input.
 And Instead of creating this list object every time we need to do that,
 I just created here once,
 and we'll clear it and fill it as we need.
 In update, the first thing we do is we check to see if we have
 input on any of the axis that we're using to blow up teddies.
 And we do that because the documentation for
 the FindObjectsOfType method that we're going to call,
 says that it's slow.
 And so, we only want to call it if at least one of those axes has input.
 In the body of the if statement,
 the first thing we do is clear that list,
 so it starts as empty.
 Here's where we call that method that is slow.
 And we say to the game,
 go find all the objects in the game that are GameObjects,
 and that returns an array.
 And now, what we can do is call another list method called, AddRange.
 So instead of just calling add with single elements we want to add,
 if we have an array for example,
 it applies to other things too like another list.
 But if we have an array,
 then we can just call AddRange,
 and it will add all the elements of the array to our list all at once.
 Now that we have a list of the GameObjects we can
 go through the process of blowing up the teddy bears of the appropriate colors.
 So, if our input axis says,
 blow up yellow teddies with the left mouse button,
 we're going to call another method called BlowUpTeddies,
 and or passing in the color and re-passing in the list of GameObjects.
 And we do the same things for green and purple,
 but you'll notice we're always using the same method, the BlowUpTeddies method.
 That's a good way to reuse the code that goes through all the GameObjects in the game,
 and only blows up the ones that are the desired color.
 You'll notice that I used if,
 if, and if here,
 because I want to make it,
 so if the player holds down multiple buttons,
 they're blowing up multiple colors of teddy bears.
 Let's look at the BlowUpTeddies method.
 Remember, we pass in the color in the list of GameObjects as parameters for this method.
 Now, we're going to do a for loop over the list of gameObjects.
 And this is different from the for loops that we've looked at before,
 because it works backwards.
 It starts at the last element of the list,
 and goes as long as i is greater than or equal to zero,
 and we decrement i each time.
 Going through a list backwards is always a good idea if you
 think you might remove elements of the list as you go through the list.
 If you go forwards through a list,
 and remove elements as you go forward,
 you actually end up skipping over elements of the list,
 and we certainly don't want that.
 So, we're going to go backwards instead.
 It turns out that the code here doesn't actually remove those GameObjects from the list,
 but sort of conceptually,
 we are destroying those game objects,
 so it's certainly possible that we'd actually
 remove them from the list as we went along as well.
 Now what we do is we get the spriteRenderer for
 the current element of the list that we're processing.
 And if the spriteRenderer isn't null,
 so we need this check because
 the main camera is in fact a GameObject that is added to this list.
 And the main camera of course is not a teddy bear we want to consider blowing up,
 and the main camera doesn't have a spriteRenderer,
 so we can use this if statement to avoid trying to process the main camera.
 If we didn't do that, then here,
 spriteRenderer would be null for the main camera and then we'd be
 trying to access the sprite of the null spriteRenderer,
 and we know that that gives us a null reference exception which is never a good thing.
 Okay. Now, we have the sprite for one of the teddy bears.
 The current element that we're processing in the list that is a teddy bear.
 And here, we do a complicated Boolean expression that basically says,
 if the color were trying to process,
 that parameter that got passed in,
 matches the sprite as green, or,
 we're trying to process purple and it's a purple sprite for the current teddy bear,
 or we're trying to process yellow and the sprite is the yellow teddy bear sprite,
 that means that we just found
 a teddy bear that is the color that we're trying to destroy.
 And we call another method called, BlowUpTeddy,
 and pass in the teddy bear that we're trying to blow up.
 The current element of the GameObjects list.
 BlowUpTeddy is simple.
 It just instantiates the prefab,
 and destroys the teddy bear.
 So really, that's the meat of
 the heavy lifting in the implementation of the game play in this game.
 To recap, in this lecture,
 we saw how we can use a list and a for loop to process the GameObjects in our game.
 And we also discussed the fact that if we might be removing elements from that list,
 we can't use a for each loop,
 and we also can't use a for loop that goes forward,
 we need to go backward to make sure that we don't
 skip any of the GameObjects we're trying to process.
Â