I’m following the Laser Defence program and am getting an exception error : ArgumentOutOfRange parameter index. I don’t know where the error is, can someone help?
Welcome to our community!
ArgumentOutOfRange / IndexOutOfRange exception means that the code tried to access a non-existent index in an array, List or another collection. The first index in an array and List is 0, hence the last index is the length minus 1. For example, if the array has got a length of 2, and the code tries to access index 2, you will get this error message because there are only two “slots” in the array: at index 0 and at index 1.
Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?
- Forum User Guides : How to mark a topic as solved
Hi, thanks for the reply. I’ve gone over my code multiple times and see no difference there. I’m thinking maybe unity game objects are not set correctly. The error is caused by the Move() function and there is lots in there. Also this code reference is all over the place and I find it hard to follow where it is going. I’m assuming the error is in reference to the waypoints[waypointIndex] and it it this array which is causing the exception error. To be honest, this function is a bit of a nightmare to follow as the error isn’t specific enough to find. I’m thinking maybe the error isn’t in the changelog code but outside it somewhere.
waypointIndex respectively sounds like a good start. Since arrays start at 0, the last index is (n - 1) where n is the number of slots/elements in the array. If the exception gets thrown here:
waypoints[waypointIndex], the issue might be caused by
waypointIndex. And this assumption is true, the issue is very likely a missing restriction. In C#, we usually use if-statements/conditions to prevent something from happening.
The first thing I would do is to log values into my console, for example, the number of elements in
waypointIndex. For me, the first step is usually to get an overview of what I have before I get into the details. As you said, the code is relatively complex. Programmers are usually not eager to read code from the top to the bottom unless it is absolutely necessary.
public List GetWaypoints()
var waveWaypoints = new List();
foreach (Transform child in pathPrefab.transform)
Yes. I think it is something to do with the config. The for each loop in the list is horribly cryptic. Also, when I put in a debug log in start, it gets called twice. I can’t see why it gets called a second time before getting lost in the List array setup loop. Is it reading the config file wrong? I can see how the config script is useful, but surely there is an easier way to do this?
That piece of code looks fine. It just adds child game objects to the empty List that was created at the top of the GetWaypoints method. Since your code does not access the List here, the problem is not here. IndexOutOfRangeException errors get thrown where the code tries to get an element from the List.