In this lecture, you'll learn how to apply those array ideas we talked about in the previous lecture in our C Sharp code. This is our game from the stop the madness lecture. When I run the game, I have a character following the mouse around, and when I left-click I change the character. In this lecture, we're going to change our character, change our script. Let's go over to that script. What we're going to do is instead of having four separate fields for our prefabs, we'll have an array of prefabs instead. I'm still going to want to populate that array in the editor, so I still need to include the serialized field attribute. The way we declare an array variable is we put the datatype that each element of the array will be, and then we put an opening close square brackets so the compiler understands that that's an array. Now we give it a variable name, and I'll call it prefab characters. I regularly name my array variables as plural with an s on the end because my arrays hold multiple elements. I'll have multiple prefab characters in my prefab characters array. I'll save my changes. Back in the editor, you can see my character changer component now has prefab characters and I can expand them. It says it's a list, but it's actually an array. I could change the size here to four. Then I would have four elements of that array that I can populate in the usual way by dragging them over from the project window onto each of these fields. I don't like doing that because then I have information in the editor that isn't actually shown here in my source code. It's much more common for us to actually create an array object when we declare our array variable, so I'm going to do that. I'll say new game object. Right between these square brackets, I need to say how big will the array object be that I want to create. I want to create it with four elements. I'll save again. Unfortunately, I have to set it to four here as well. This is a manual process. It doesn't hurt me to put the array object creation. Remember arrays are objects, that's why I used new. I wanted to have my source code reflect the size of the array, but that doesn't actually come over here to the editor so I still have to change it to four. It's personal preference that I like to include the actual size of the array over in my source code. Let's populate the array by dragging each of my prefabs onto each of the elements of the array. Because I'm not using the array yet, this just keeps working the same way it did before. When we come back to our code, I can now get rid of these four fields because I'm storing four elements in my array instead. Unfortunately, that gives me a bunch of compilation errors now. Let's start working through them. The first one is on line 25, and I can double-click it to go there. That's because prefab character 0 no longer exists. We have an array called prefab characters, but I can't just pass in the entire array to the instantiate method, I have to pass in a specific game object. So we need to access one of the elements of the array. We do that by using square brackets. Then between the square brackets, we put the index of the element that we actually want to access. In this case, I'll just access the 0th element of the array. Normal human beings would call that the first element in the array, but it is at Index 0. Now I've fixed this compilation error because I'm passing in that 0th element of the array, which is a GameObject, into the instantiate method. This doesn't really look like a big win at this point. We haven't really gained anything by using an array. We're about to see where we gain a lot by using the array. Down here in the update method, when I decide it's time to change characters, here's where I'm instantiating a new character. I'm generating a random number that's 0, 1, 2 or 3, and then I have this if statement that instantiates the appropriate character based on the random number I generated. Instead of doing it this way, I'm going to copy this code right here. I know that I can't do this. I can say prefab characters. Then I need to put the index of the prefab that I want to instantiate between these square brackets. I'm going to move these other two arguments down the line because I can just take this code and put it here. What will happen is I'll call the instantiate method. I'll say one of the elements of prefabCharacters, and it will be the element at index 0, or at index 1, or at index 2, or at index 3. Once I do that, I can delete all this code. That's a huge win for us, that makes this code much more compact and it's still easy to read as long as you understand how arrays work. I should be able to build the down and I can. When I come back to the editor and run the game, it works just like it did before. I can move around. I can left-click to change characters and so on. I want to show you one more thing before we end this lecture. While it's often useful to populate elements of the array from within the editor. Sometimes we'd rather just do it from within the script instead of doing it from within the editor. Especially if we have a lot of prefabs to populate or something like that. I'm going to change this so that we actually load these prefabs into our array as the script runs, rather than doing it from within the editor. The first thing I'm going to do is over here in the assets folder, I'm going to create a new folder called Resources, and it has to be spelled exactly that way, and it has to start with a capital R. Now I'm going to take my Prefabs folder and I'm going to drag that into the Resources folder. Now I have this folder of prefabs essentially marked as resources that I can load from scripts as I run those scripts. Back in our character changer, I no longer need to mark this array with serialized field because I'm not going to populate it in the editor. Instead, I'll populate the character array here, and I'll say character prefab array. Here's how I can do that. I've already created an array object that has four elements. I can say prefabCharacters 0 equal, and now I want to load one of my prefabs. I say Resources.Load, and I'll say GameObject. Because I am loading a GameObject, I'll add my semicolon now, and I need to provide a string. I need to provide the name of the game object I want to load. These are actually called HunterMan, MinerMan, PioneerWoman, and SkunkWoman. But they're in the prefabs folder, and I know I'm going to run off the end of this line, so I'll bring this down. I can say Prefabs/MinerMan, like that. Now, the direction of this slash matters. If I use the Windows slash like this, it turns out that I get a compilation error, and that compilation error says "Unrecognized escape sequence." When we actually learned about strings, we'll learn about something called escape sequences. There are two ways to avoid this error. One way is to just go like this, and I will show you that all works, and then we'll come back and look at the other way to do this. Now I've loaded all the resources for the elements of the array, and I realized that HunterMan was my first one, not MinerMan, so I fixed that. We're loading HunterMan into the zeroth element of the array and MinerMan into the first element and PioneerWoman into the second and SkunkWoman into the third, and when I run the game, it behaves just like it did before. This works great, and that solves that problem. I just got th three of those PioneerWomen in a row. The other way you can do this, if you don't like using the slash in that direction, you want to use it in this direction instead. With an ampersand at the beginning, you can say, I mean this string exactly like I've typed it. Don't treat this as the start of an escape sequence. Just treat this string just the way I've given it to you. I'll actually do it that way here. But either way works just fine. Building one more time, and back in the editor when I run the game, it still works like it did before. That's how we can convert from using multiple fields for our character prefabs to using an array instead. We saw that made our code much more compact as we were randomly picking a character to change to, and we also learned how we can dynamically load resources, the prefabs in this particular example as we run our script. To recap, in this lecture, you learned how to implement and use arrays in C Sharp. You learned that we need to specify the size of the array when we create the array object and independent over arrays, you learned how to load Resources from our scripts as our game runs.