List out of range on parameter index; Editor in debug shows it isn't
Hello,
I am getting an error when accessing the zeroth index of a list I am working with. This list is a clone of a public static list in another class. I can access currentHyperlane[0] with debug.log and print out its contents, and I can see that there is a transform in the 0 element when editor is in debug mode.
However, in the code below I throw an out of range exception when I try and set a transform to the zeroth index. I have been searching around all afternoon and cannot figure this one out. Any help is greatly appreciated thank you!
private void Start ()
{
currentHyperlane = new List<Transform>();
enemy = GetComponent<Enemy>();
PickHyperlane();
}
private void PickHyperlane()
{
whichHyperlane = useHyperlane.tag;
switch (whichHyperlane)
{
case TAGS.HYPERLANE1:
currentHyperlane = Waypoints.hyperlane1;
GetNextWaypoint();
break;
<...Snip...>
void GetNextWaypoint()
{
Vector3 currentPOS = this.transform.position;
Transform endpoint = currentHyperlane[0]; //We get out of range exception error here
Transform originPoint = currentHyperlane[currentHyperlane.Count()];
currentWaypoints = new List<Transform>();
<...Snip...>
Answer by MacDx · Oct 19, 2017 at 10:25 PM
There's nothing wrong with the code you are showing here. What's probably happening is that Waypoints.hyperlane1 is not getting you what you want, maybe somewhere else it is being modified before you access it in this script.
¶
My suggestion is to add a bit of debug code so you can see what's going on. Try adding this little snippet between the assignment of currentHyperlane and the call to the GetNextWaypoint method. You would end up with something like this:
¶
currentHyperlane = Waypoints.hyperlane1; //Assignment of currentHyperlane
//Debug code
foreach(Transform t in currentWaypoints)
{
print(t.gameObject.name);
}
GetNextWaypoint(); //Call to Get Next Waypoint method
¶
That way you will be able to see everything that is inside the currentHyperlane list before trying to access it. And there will be 3 possible scenarios.
¶
1) The list will be emtpy (you'll see no prints). Meaning that Waypoints.hyperlane1 was empty.
2) The list will be null (you'll see a null reference error). Meaning that Waypoints.hyperlane1 was null;
3) You will get some objects (you'll see some prints). Meaning that Waypoints.hyperlane1 has the correct stuff inside.
¶
Hope this helps!
Well I ended up with Scenario 3 when I looped through printing the contents of the list both in the class it originates in AND just before calling GetNextWaypoint(). Additionally, the contents were the same in both cases so nothing seems to be modifying the list. This did actually help me fix an unrelated bug so now my waypoints are all being inserted though so thanks for that lol.
At this point I am still getting out of range errors when I try to assign a transform to the value of currentHyperlane[0] saying it is out of range even though there is a transform in the 0 index.
I made these original lists of waypoints for each hyperlane static so that there would only be one set of them since it is an unchanging component of my scene that have been manually placed. I will try removing their static designation and making a private copy of the waypoints class on my enemy just to see if I can get this work though I don't see where it should help me and unfortunately would mean every spawned enemy has to iterate through and make their own lists which is what I wanted to avoid.
Thanks for the suggestions and if you think of anything else please let me know! @$$anonymous$$acDx