Important Notice:

As of September 15, 2016, we no longer publish new courses on this website. We encourage you to use your Digital-Tutors credentials to sign in on Pluralsight where you'll find all new creative courses, skill tests and paths, 1:1 mentoring and more.
Understanding arrays
See Details
Joshua Kinney


Joshua Kinney
In this lesson, we're going to learn how to use arrays. All right, so in games, arrays are commonly used in inventory systems. And the best way to explain an array is going to be it's a list. If you try to make arrays any more complicated than that, it's going to be very tough for you to understand. Arrays are just lists that we can add items to a list, and we can remove items to list. Now, as far as how we do that in code, that's where some of the difficulty comes from. But with this, what we're going to do is we're going to break it down very simply, and we're going to learn how to, first of all, create a list of items, and then, we're going to learn how to check a list of items. And then, we're even going to learn how to add a list or add an item to the list. So let's go ahead and get started. Go ahead and create that lesson 10 script, open that up in MonoDevelop, and also have lesson nine open. And what I want to do is I want to borrow some scripts or some code from this script here. So let's come in and let's grab everything, let's grab this public weapon, and let's go all the way down to just above that display message there. So I'm going to copy that. Let's go to lesson 10, and I'm going to paste this and replace everything here. So I'm just going to right click and Paste. And we want to make sure that we have all of our parentheses and our brackets closed, so we'll add one more to the end there. Here we go. And we had a couple of values and variables here that really aren't needed. So we have this weapon name, believe that's going to be in the way. The distance, let's take that out. I'm just checking through this really quickly. Enemy search, I don't need that. And we should be OK. Now, display message is going to give us an error, but we'll worry about that here in just a little while. So what I want to do is I want to get into creating the inventory system itself. I want to create the array. So to get started with this, I'm going to go right up above my weapons search, and I'm going to type in void check inventory. And give myself some room here. And in our check inventory, we're going to create our array. Now, before you create an array, you need to decide on what data type you're going to hold in this array. So with that, we're going to use a string data type because we're going to be placing in just simple words. So I'm going to type in string, and then I'm going to do an open close bracket to tell this that it's going to be in an array. We're going to give it its identifier or its name. We're going to call this inventory. And then, we're going to set this equal to a new array. And so what we're doing here, on this side, is we're actually constructing the array. So we're going to say new string and then, open bracket, and now, we need to tell it how many elements we want in this list, in this array. So an element is basically just a compartment in the list. So if I want five sections to my list, I can only hold five objects. So I'm going to type in five here. Now, if I want to list that holds 10 objects, I would simply replace that with 10. So I could even put a variable in here if I wanted to. So, let's say, I want a variable called inventory space. I could do that. Now, I'll have to come up here and I'll have to put in a variable. So I'm going to put in public int and then inventory space. We'll set this equal to five for right now. All right, so this will work just as well. And this will actually allow you to change the inventory space. All right, so now that I have this set, I have new string inventory space set up, now, we need to tell it what to actually put into our inventory spaces. So to do this, we're going to do a curly bracket, so open curly bracket, and then we're going to put in a string, which is going to be our first object. And this is going to be in the element of zero. So each element that you have in your list, the first one is the zero place. The second one is one, the third is two, and so on and so forth. So this one, we're going to type in ripped underscore tunic comma, and then we're going to put in old boots comma, and then we'll do dagger. Make sure that we set that up as a string, and then, on the fourth one we're going to do gold ring. And then, on the last one, I want it to be an empty space. So to create an empty space in an array, you simply type in null. Let's close that list on our array. And now, we are done assigning objects to the array. Now, what I need to do at this point is I need to be able to iterate through the array just to check to see what's in it. So we're going to use a for each loop here. So foreach, all one word, and we're going to open parentheses and we're going to say for an each item in the inventory. We want to iterate each item in the inventory here, so for, and we're going to say string items in inventory. Now, in this for loop, let's go ahead and let's tab that across correctly. So for each inventory, we're going to-- or actually, let's just debug this. Let's actually just write something to the log. So let's do debug dot log, and we're going to say items. So we're going to pass in that variable there. So it's going to just display what is in our inventory on our console. OK so now that we have that, let's go ahead and move on. Let's check our weapons search. So what is it we're actually wanting to do here? Well, what I want to do is I want to be able to press any key on the keyboard, and I want to be able to check my inventory and I want it to bring up a list of what's in that inventory. So on my update here, I'm going to put in another if statement here. I'm going to say if input dot get key down-- or get key up, I'm sorry. And we're going to set that to I. I want it to do this. So I'm going to go ahead and have it do check inventory. And we'll go ahead and close that if statement there, and there we go. So whenever I hit the I key, it's going to run through my check inventory, it's going to create a new array, and it's going to place these objects in it, and it's going to leave an empty space. And then, it's going to iterate through that checking to see what's in there and then it's going to spit out a list in my debug console. All right, so let's go ahead and let's save this out. And we're probably going to have a few errors and things like that in here. So let's just check this out. It's saying a constant value is expected here. So looking at this, it's saying string inventory new string, and it's not liking that variable there. Let's go ahead and just replace that. Let's say five, and it's going to be a constant. We don't want that to change. Let's go ahead and move that. I forget that we're using a C# array, so it's not dynamic. So let's go ahead and go to Unity. And that gets rid of that error, and now, we have the name display message does not exist. So what I'm going to do is just comment out those lines of code for right now, because I'm going to come back to those. I'm actually going to use those later on. So now, let's save this. It's going to get rid of those errors and then we have a warning here, and it's saying that the weapon name variable is not used. And we're going to leave it just the way it is. Let's hit Play on this, and we're going to hit I on our keyboard. And you'll notice that nothing is happening here. Let's hit Space. OK, nothing is writing, and the reason for that is because we didn't assign our script to the game object. Let's go ahead and close that, assign that. Here we go. Let's hit Play, and now, let's hit I on the keyboard. And now, we'll see that we have our list of our inventory here. Let's go ahead and close this. So now that we've created an inventory, let's learn how we can actually add to that inventory. So to get started with this, let's go ahead and use our weapons search functionality. So what I want to do is whenever I press the Spacebar, I want to go ahead and generate a random number. And then, whatever number it lands on, I want it to take that weapon name, and I want it to update the inventory. So I'm going to create a new function down here. And I'm going to call this void update inventory. Let's open parentheses and I'm going to pass in a variable, and I want to call this weapon name. So if you'll remember from the last lesson or the lesson before that, I can't really remember, the display message weapon name, we're passing in that variable there. So let's pass this in, and we're going to create a variable called added items. And we're going to set that equal to weapon name. And then, we're going to go ahead and call our function, so we're going to say check inventory. And we're going to pass along the added items variable, which is going to be our weapon name, which is going to be passed from our weapons search here. Let's go ahead and close this function. So that's good. So once we update the inventory, a couple of problems that we're going to see here is that we don't have this added items variables. So let's go ahead and create that really quickly. So up underneath here, we're going to type in private and string added items equals null. So that is now set to nothing. And now, let's go ahead and let's check our update, so everything is set up perfectly here. Now, we want to pass in that added items in our check inventory. So we're going to pass that in. So added items and you'll see that it's linking up here. So it's working great. So that's going to be passed into our check inventory. And once it goes to the check inventory, we're going to tell it what it's going to be passing in, so string added items. And it's going to pass this in, and what I want to do is I want to assign that added items object to the number four element of my inventory. So we're going to go into our for each string here, or loop, and we're going to say inventory. And we're going to tell element four, which is our null, and we're going to set that equal to added items. So let's go ahead and save this. Let's go into Unity and just check. And it looks like we're still having an issue with that weapon name. It's saying that it's never used. So we know that that's not true, so let's go ahead and check to see what is going on here. See weapon name is set to null. Here's our weapon name here. We have a little bit of a disconnect here. Let's see exactly what is going on. I see it here, so I'm not really sure what it's talking about there. Maybe that was from last time. Let's go back to Unity, and let's open up our console, and let's just clear that out. And let's go ahead and play, and I'm going to go ahead and check my inventory, so I'm going to hit I on the keyboard, and you'll see that we have our list. We still have that null object there. Let's go ahead and hit the Spacebar to search for a weapon, and once we do that, it should go ahead and check that inventory. Now, let's go back to Unity here because it's not checking that inventory. So you might be thinking, I know exactly what's happening here. Whenever we go to our weapons search, what it's doing is it's not being called whenever we hit the Spacebar. It's not giving us the proper result that we're looking for here. It's not sending out the correct message, so here it's not being called. So this is what we've done wrong. We have this display message, and we have that weapon name, so what we want to do is we want to call out that function. So we want to send this out to the update inventory. So we're going to do that on all of these. And let's go ahead and just copy and paste this. There we go. Let's go ahead and save that. Let's go to Unity. Let's clear this out, and I'm going to hit Play, let's hit I, check our list. It's null. And let's hit Space, and you'll see that that list has been rebuilt. And now, we have ripped tunic, old boots, dagger, gold ring, and then, now we have that sword in that null space. All right, so this is just a basic look at how to create arrays and how to add items to an array. Now, in our next lesson, what we're going to do is we're going to take what we've learned, and we're going to put that into project form by creating just a simple move script.
In this series of Unity tutorials we'll discuss the major foundations of scripting with C# in Unity and apply what we've learned in two mini projects.

To start out, we'll look at several of the terms and techniques that are used when scripting in Unity such as creating and manipulating variables, understanding the different types of operators, and how we can create instructions for our game objects using functions. We'll also jump into creating logic with conditional statements, and loops. We'll even learn how to use basic arrays. Finally, we'll take what we've learned and apply it to creating a movement and animation script.