 In episode 2.3, I'm going to be teaching you guys what arrays are and how to use them and when you're going to be using them. What is up everyone? It is CryptoGround here. Welcome back to another Unity Adelgame tutorial video. This is episode 2.3, and this is part of the 2021 edition, and today we're talking about arrays. So arrays are going to be a huge part of our development here, and you're going to be using them all the time. I promise you that. I don't think I've made a single project where I haven't used in a single array. Trust me, they're a lifesaver. It prevents a lot of hard coding and copying, pasting, and just a bunch of repetitive stuff. So some of you may actually not know what arrays are. Basically, they are a data structure where you can hold one or more variables in the same object. So this can be very useful when you have like multiple of the same type of object. Like for example, you have three different levels or three different names. For example, you can just put all three of those into a single string array or a big double array or something like that. And it's just one variable, and they're very easy to use and they're very easy to understand. So this episode is going to be very important. I encourage everyone here to be watching this video because we will be using them starting in episode 2.5 when we add more upgrades, and we're going to be using them further on. Anyways, if you enjoyed this video, make sure you smash the like button. Consider subscribing to my channel if you are new around here. If you're enjoying the content and also turn on the bell for future notifications of videos and live streams. I'm going to be trying to pick up the schedule within the next few weeks. So we'll see, hopefully I can stream and make videos as much as I can. Anyway, let's just jump right into the arrays. So in this video, I'm only making examples. So these are not going to be part of our game and we will start doing that in episode 2.5. So I'm just going to create a few example arrays just to show you guys how to create them and how they work. So there are two ways to create an array or to specifically initialize them. And I'm going to show you the first one, which is to initialize it by size. So what that means is that arrays, once you create them and you set a size, they cannot expand. You can use a small chunk of the array. Like for example, you create a size 5 array, but you only want to use three of those for some reason. That is perfectly okay. You don't have to use all five of them. However, you cannot create another one in order to make it a size 6. You'd have to create an entirely new array. So anyways, let's just create this array. So I'm just going to do this for example, let's call this public string. Okay, so now to make this an array, we have to put our brackets here like this right after string. So that's how we make it an array. And then we're just going to call this our name string, just for an example. So now this is a string array, it's empty, it hasn't been initialized yet. So once we start trying to use this, it won't work because it hasn't been initialized yet. So let's do that. So how do we do that? First of all, we're going to actually do it by size. So in our start method here, I'm just going to do name string equals new. And then we can do string with our brackets here. And in here, we can put the size of the array we want it to be. Let's just say for example, five. Now what it's going to do is that it's going to create a size 5 string array and all the slots are going to be empty. So what it's going to look like is something like this. Okay, so it's just going to be empty strings, five empty strings inside this array. Okay, so that's pretty much it. So now let's say you want to actually start an array with certain strings and never have to touch them. This would be good for like upgrade names, just for an example. You want to pre-define some names and you don't want to touch them ever again. We can do that in our start method and that's just a really easy thing to do. So what we can do here is get rid of the size here. We could just leave it empty. And then we're going to add curly braces here. And then we start adding our strings here. So for example, I can just do hello comma world. And then I don't have to put a comma here since this is the end of the list. So here we have initialized our name string as a size 2 string array and it contains the strings hello and world. Now again, we cannot change the size of this array. However, we are able to access and change the strings inside of this array. So since these are strings already, we are able to get rid of this string term here and just leave it as a new and then brackets here. But let me show you guys something real quick. Let's create a big double array here. And I'm just going to just call this example. So now let's just say we do equals new brackets, curly braces here. And let's just make a list of like one, two, three. So we're going to run into an issue here because one, two and three are ints. They are not big double. If this was an int array here, this would be perfectly legal. We got to put the semicolon here. By the way, so forget that. This would be perfectly legal because these are ints and C sharp knows this already. However, since we're using big double here, we must add the big double term here just so we can tell C sharp. Yes, we are creating a big double array, not an int array because a big double array is not the same thing as an int array. There are two different types. So I hope that made a little sense. Okay, so now that we've created our first array here, how do we access them? Well, well, it's pretty simple here. So all we got to do is print. So I'm going to be printing this for example. This will be the exact same thing as setting a text to this as well. But I'm just going to print it into the console instead. So all we got to do is type name string, put our brackets here and then a number in here to determine the index. So now a really important thing you guys have to know is that arrays are a zero index base. So basically it starts at zero and then it starts at one. It's not one representing the first one and then two. It doesn't work like that. It starts off with zero and then one. So if we try it now, if we try to put in two, it's not going to work. We're going to get an error and I'm going to show you guys that in a little bit. But for now, let's try to access world. So I'm just going to put the number one here. So right now we're trying to access the first index, which is world. And this would be the zero index. So now if we play our game right now, we should see world in the console. Perfect. So we see world here. So now if we change this index here to zero and save it, we should see hello now. Very nice. Now, if we were to combine this all together, we can just do plus space here, a space string here and then do name string index one. We can access both of these in the same line. So now we should see hello world. Hello space world like that. So now you can actually change strings inside here too. It's the same thing as accessing them. You do the same format here, except you treat it as a variable. So we're going to change world to universe, for example, or actually, we're going to do hello YouTube. So we're just going to set this first index here. We're going to replace world with YouTube, for example. Okay. So now what it should do, it should print hello space YouTube. So let's save it. Cool. So now it says hello YouTube. And once again, you're totally allowed to change these whenever you want. So you can just change this to subscribe to subscribe to crypto grounds, please. I recommend that. So now in the console, it should say subscribe to space crypto ground smiley face. Cool. So now we know how to actually use arrays, but I need to show you guys some safety precautions here. So what happens if we go into the negatives? Let's just try negative one. So we're just going to get an error here and I'll show you what that looks like. So you see here, I have an index out of range exception. Okay. And this pretty much screws up our entire game because now we're just getting a bunch of no reference errors for everything else. So once we have one error like this, it could potentially just mess up everything else. And the reason why we're getting this no reference exception is because of this data flash here. So this may be a little advanced for you guys, but basically we got this error right here and it just skipped all of this code and it just kept running the update method. And remember, we didn't assign data yet. So it pretty much just ignored all of this. But yeah, so we have a no reference error because data was never assigned because this pretty much skipped it. So one major lesson is to make sure you never use negative numbers as your indexes because they're never going to work. They're never going to work. It's zero or positive only. So now what if we do two? Okay. So if we do two here, we're going to get the same index out of range exception. And the error pretty much just says index was outside of the balance of the array. So the balance of the array is from zero to one. Zero is the minimum and one is the maximum. Now, if we were to add another one here, let's just do like lol, for example, the balance is now from zero to two. So this would work just fine right now. So now if we run it, it should just print out lol, and it'll work perfectly fine. Now, however, if we got rid of this one right here and just kept it with these two here, we're going to get that same error. See, we got the same error here. Basically, just make sure you never use an index that is outside of the bounds of that array. So one of the variables that comes with arrays is length. So here it's going to print out the size. And now here it's going to tell you the size is two, not one. So now the difference between length and the maximum index here is that length is just how many variables are inside this array. So this should print out two. And we will still get the same error here. Yeah, see, we got two. So some interesting things you can do here is you can iterate through your arrays here if you want to print out everything. So to do that, we can just create a for loop. So now we have a variable here with a semicolon. So we're just going to start at zero. And we basically don't want to go past the length. So basically, it's going to iterate from zero to one. And it's just going to stop. And what we can do in here is just print name string. And then our index is going to be I. So now we're just going to print, subscribe to, and cryptograms please on two different lines. Very nice. Another way of doing it is for each statement. So you can just do for each. And what you can do is do string I in name string. And what we can do here is print our I here. So now we're basically just going to print for every string that is in this array, we're just going to print whatever that string is. It's kind of like a for loop. This is very good for wanting to just iterate through the entire thing. So now if we run it, we see the exact same thing because it is pretty much doing the same thing. So once again, we're going to be using these starting episode 2.5 and beyond promise you, we're going to be using them every single episode. So make sure you study them, make sure you fully understand how they work. And if you need to understand more on what they do, you could check out link in the description below as I'm going to provide the docs for you or you can just watch some other YouTube videos. So in the next video, we're going to be talking about lists. So in the next video, we're going to be talking about lists. So what's the difference between arrays and lists? Well lists, we can actually add onto them on like arrays. So the really unique thing about lists is that if we know that we're going to add more in the future, like we're going to add more upgrades in the future, okay, then we should probably use lists so we can easily expand them later on, just add more upgrade levels, stuff like that. While arrays, they need to be set and done unless you create an entirely new array, which can be quite a hassle. I'm going to be explaining more in depth on how lists work in the next video, but basically just know that lists are probably going to be the most common things we use compared to arrays here, just because we're going to be able to expand them easily and they're pretty much easier to mess with. So anyways guys, if you have any questions, please let me know in the comments below. If you enjoyed this video, if you learned something new, make sure you smash the like button, send this video to some friends who may be interested in this kind of stuff or who wants to learn about arrays as well. Subscribe to my channel if you're new around here and if you enjoyed this content, make sure you turn on the bell for future notifications of videos and live streams. I will be going live every once in a while and I'll be sure to post videos as much as I can. If you want to support my channel, consider clicking that join button below or checking out the Patreon and the link in the description. Anyways, I hope you guys all have a fabulous day and night. I'll catch you guys in the next one and thank you very much for watching. Have a good one. Peace.