ArgumentOutOfRangeException: Argument is out of range. -> Index
Hi,
for some reasons I can't get the index ArgumentOutOfRangeException solved. I tried variations of the condition (< oder <=) and the post/pre increment and other stuff. Tee strange thing is; this functions worked until I packed them together in one Method, but I see no problem as they're individually calculated in their swich cases.
ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.Vector3].get_Item (Int32 index) (at /Users/builduser>/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) Squad.buildFormation (FormationType type) (at Assets/WorldObject/Unit/Squad.cs:39) Squad.Update () (at Assets/WorldObject/Unit/Squad.cs:22)
Here's the code:
public void buildFormation(FormationType type)
{
List<Vector3> pts = getFormationPoints(type, null);
Transform pivot = squadMembers[pivot_id].transform;
Quaternion look_rot = pivot.rotation;
for (int i = 0; i <= squadMembers.Count; ++i)
{
squadMembers[i].transform.position = pts[i];
squadMembers[i].transform.rotation = look_rot;
}
}
get FormationPoints:
public List<Vector3> getFormationPoints(FormationType type, Transform destination)
{
List<Vector3> pts = new List<Vector3>();
Transform pivot;
Vector3 offset;
switch (type)
{
case FormationType.Line:
pivot_id = squadMembers.Count / 2;
if (destination != null)
pivot = destination;
else
pivot = squadMembers[pivot_id].transform;
offset = pivot.position;
offset = offset.normalized * distance;
for (int i = 0; i <= squadMembers.Count; ++i)
{
pts[i] = pivot.position + offset * (i - pivot_id);
}
break;
case FormationType.None:
// smth
break;
}
return pts;
}
The problem occurs at both foreach block but if I debug the lenght of the arrays it returns an Integer.
if (squadMembers.Count == pts.Count)
wrapped arount the loops would return fals, which is strange.
The squad members are public lists for the class:
public List<Unit> squadMembers = new List<Unit>();
Answer by Commoble · Mar 06, 2017 at 07:00 AM
Change
<=
to<
in both of your for loops. You do this in the first one because the last valid index in an array is equal to itsCount-1
, so you want to stop the loop before you it tries to use theCount
as an index. You do this in the second one because pts has to be the same size as squadMembers in your setup.Either make pts an array of Vector3s instead of a list, or use pts.Add(item) if you need it to be a list. I suggest the former.
Yup, the <= was just the latest test, I still use <. I already changed the point with the array but this doesn't work, either
Answer by LiloE · Mar 06, 2017 at 12:48 PM
You can't access list members without adding them first.
Try this:
Change getFormationPoints to start like this
public Vector3[] getFormationPoints(FormationType type, Transform destination)
{
var pts = new Vector3[squadMembers.Count];
And in buildFormation
Vector3[] pts = getFormationPoints(type, null);
Your answer
Follow this Question
Related Questions
Does anyone understand why I keep getting "ArgumentOutOfRangeException" from my code? 0 Answers
UNET ArgumentOutOfRangeException 0 Answers
Class com.playerio.PlayCodeStorage not found... 0 Answers
How to correctly handle NullReferenceException when using GetComponent<>? 1 Answer
Problem when acessing a list from another script? (ArgumentOutOfRangeException) 0 Answers