Remember that, although we offer solutions to an error you are receiving. Our solutions might not perfectly fix your bugs every time. If your errors persist, do not panic First tried the following steps, search our other debug videos for additional help. Leave us a comment Explaining your problem in complete detail, search the official unity sites for more help and finally search Google for other possible solutions. Enjoy the video. Hey, everyone, how’s it going? Welcome back to another debugging tutorial in this video we’re going to be talking about arrays and the index out of range exception error. So let’s get started now before we begin. Make sure that you subscribe to our Channel and hit the notification Bell. So you can get updates when we publish new videos so here. I have just a basic testing environment opened inside of unity and right now I just have one script, which I’ve called test and that script is attached to an empty game object in our scene and let’s go ahead and open up the test script. Now within the script, I’ve written a number of different lines of code, which will each give us this index out of range exception error when we use these lines of code improperly and so we’re going to go through each one of these lines of code, and I’ll explain why it’s throwing the error and how to properly use these lines of code in order to not receive that error, and so the first line of code is where we’re taking the first index of our number array variable, which is just an array of integers and we’re saving that value within the my number variable, which is just an integer variable. So I’m gonna go back to unity where we can test this line of code now. I’m gonna go ahead and hit play because I’ve already setup an empty game object with our test script attached to it. So when I hit play because these lines of code are being called in the start function, we immediately get an error that says index out of range exception, and then the message says array index is out of range. And you can see that it’s being called within our test script and the start function now. This type of error is what’s called a runtime error, which occurs during the execution of your game and so this error won’t prevent your game from running, but it can seriously hinder the process of your game and even cause your game to crash one last note that I’d like to make about this particular error before we get into the cause and solution is that this error is a common error that is not unique to just unity and c-sharp, but it can actually occur in many different coding languages that offer array type variables now the cause of this error deals with the size and content of our array and the index that we’re using to access the values within our array. Now, when I go back to our script, you’ll notice within this line of code that we’re trying to access the first index of our array and save that value into the mine number variable, but the problem is that we have not set up the array with any values. And so when I go back to unity, you’ll notice that when I select our empty game object within the test script, you can see that we have our array and the size is 0 and so anytime you have an empty array and the size of that array is 0 that will throw an error when you’re trying to access any index of that array, and this is true, even if you’re trying to access index 0 and so to fix this problem, all we have to do is make sure that our array, at least has that first index, and so I’ve added the size to be 1 and I can set the value of this to be anything I want, so let’s say 6 and so now when I run the game, you can see that we no longer receive any errors within the console and you can see that the my number variable has been set to 6 now. I’ve gone ahead and commented out the first line of code and uncommented the second and in the second line of code, we are trying to access the 5th index of our array and save that within the my number variable, so let’s go ahead and save this code and go back to unity now, for this example, let’s say since we’re trying to Access Index 5 that our array is of size 5 and let’s go ahead and hit play and so here you can see that we’re still receiving our index out of range exception error, and that’s because although our array has a total of five elements, the count of those elements begins at zero, so we have elements zero one, two, three and four, but we don’t have element five and so when we go back to our code, you can see that we’re trying to access element five, but there’s no such thing as element five and so if we want to access element five, then we need to make sure that our array is of size six and so let’s go ahead and hit play, and now you can see that we’re no longer receiving an index out of range exception error, and so when dealing with arrays. You want to make sure that you access it by one less than what you would traditionally think now? I’ve commented out the second line of code, and I’ve uncommented the third section of code, and this is just a for loop where we’re trying to iterate through our array and take the value at each index and save it into the my number. Raye and it might even be better. If we just do a plus equals, and that way, it adds to the my number and so let’s go ahead and save this code and we’ll go back to unity now within unity. I want to have our array. Be a smaller size as what we are iterating through, and so I’m gonna make it size three. Now, let’s go ahead and hit play now here. You can see that we’ve taken the three values of our array and added them together and then saved them into the my numbers variable because 3 times 6 is 18 but we can also see that we’re now receiving an index out of range exception error. Now the reason why we’re receiving this era is because we’re iterating through our for loop five times and each time we iterate through our for loop. Our eye index is incremented by one and so we’re trying to access elements 0 through 4 but because our array is only of size 3 that means there’s only index is 0 through 2 and so everything’s perfectly fine and we’re adding the values within our array into the mind number variable all the way up until we reach the third element and so because there’s not a third element, it throws index out of range exception error. And then it stops. It doesn’t do any more that and so a word of caution anytime your for looping through an array and you’ve hard-coded the top range value. Which is this five? You want to make sure that that value is no greater than the size of your array. It’s okay to have a top range value that’s less than the size of your array, but anytime you go over the size of the array, Then it’s going to throw an index out of range exception error and so one way to fix this error is to change our V to a three because three is the size of our array. I’ll go ahead and save that and go back to unity. Now when I play our game, you can see that we’re no longer receiving an index out of range exception error, but we’ve also been able to for loop through our array and add all the values into the my number variable now. I’ve commented out the third section and uncomment at the fourth section, the main difference between this for loop and the previous one is that we’re no longer hard coding. The top range value instead we’re using number railing, which gets the size of our array. Another difference is that we’re using less than equals two instead of just less than, and we’re also subtracting the value of the array from my number variable, so let’s go ahead and save this and go back to unity. So now when I hit play, you’ll notice that we’re still receiving an index out of range exception error. But you can also see that we’ve subtracted six three times from my number now. The reason why we receive in this error is because far less than an equals to sign, which is a common mistake that a lot of people can make, but this means that we’re going all the way to the size of our array instead of 1 less than the size, and so when we’re trying to access our array by the size we’re trying to access an element. That’s not there because the count of our array begins at 0 so it’s always going to be 1 less than the size. Now all we have to do to fix this. Error is delete this equal sign here. So our for loop should read for Int. I equals 0 I is less than number array dot length. I plus plus, and then we have our code within the for loop now. This is the best way to write a for loop. If you trying to iterate through all the elements within an array, I always use the name of the array dot length for the top arranged value because even if you change the size of the array, dot length will be that value, so let’s go ahead and save this code and go back to unity. So now when I hit play, you can see that we no longer receive an index out of range exception error, but we’ve still been able to iterate through our for loop, subtracting 6 from my number three times to get negative 18 so that takes care of the index out of range exception error. This is a runtime error that is not unique to unity or c-sharp. It can happen across many different coding languages that offer array type variables. Now there’s a number of ways this error can occur And there’s a number of ways that you can debug it. The first thing that I would check for is to make sure that your array is populated. If your array is empty or it does not have any elements. Then it’s going to throw an index out of range exception error. If you try to access it, the next thing I would check for, It is to make sure that you’re not trying to access your array by a value that’s greater than or equal to its size. Remember that the count of the elements begins at zero, and so it’s always going to be 1 less than what you traditionally would think. The next thing I would check for is that? If you’ve hard-coded the top range value of a for loop iterating through an array that that top range value matches the size of your array and the last thing to check for is to make sure that you’re only using the less than sign instead of less than equal to sign. If you’re using the size of your array as the top range value of your for loop. Now, as long as you keep the value of your index within the range of your array, you should never receive an index out of range exception error, but it is an error that a lot of beginners receive. So if you get this error, don’t feel bad. Just follow the steps on how to debug it now. I hope you enjoyed this video and that you found it to be helpful. If you did make sure that you give it a thumbs up and share it with your friends also subscribe to our channel, so you can be up to date on all our latest videos. Thanks for watching and well. See you next time you.