- Home /
IndexOutOfRangeException: Array index is out of range.
I have list and array. i think i created wrong script. I want to show data profile of building after I calculate the value of integer like matrix. but still error in array. can someone help me? thanks
public double[] _Capacity, _Equipment, _Rooms, _Parking;
private List<Mylist> MyList = new List<Mylist>();
public GameObject listPrefab;
public Transform listParent;
for (int i = 0; i < MyList.Count; i++)
{
_saw[0, i] = (_Parking[i] * wParking) + (_Rooms[i] * wRooms) + (_Capacity[i] * wCapacity) +
(_Equipment[i] * wEquipment);
_saw[1, i] = i;
}
bool flag = true;
double tmp;
for (int i = 1; (i < MyList.Count - 1) && flag; i++)
{
flag = false;
for (int j = 0; j < MyList.Count - 1; j++)
{
if (_saw[0, j + 1] > _saw[0, j])
{
tmp = _saw[0, j];
_saw[0, j] = _saw[0, j + 1];
_saw[0, j + 1] = tmp;
tmp = _saw[1, j];
_saw[1, j] = _saw[1, j + 1];
_saw[1, j + 1] = tmp;
flag = true;
}
}
}
for (int i = 0; i < 3; i++)
{
int index = (int)_saw[1, i];
GameObject tmpObject = Instantiate(listPrefab);
Mylist tmpList = MyList[index];
tmpObject.GetComponent<ListScript>().SetList(tmpList.Name, tmpList.Location.ToString(), tmpList.Capacity.ToString(), tmpList.Rooms.ToString(), tmpList.Equipment.ToString(), tmpList.Parking.ToString(), tmpList.Status.ToString());
tmpObject.transform.SetParent(listParent);
Debug.Log(MyList[index]);
}
Hello there,
You have a lot of things going on there. COuld you provide the full error message (with line numbers) so it's easier to help you? Also, try putting some debug logs in there and tell us which ones get or don't get triggered.
Thank you!
Cheers,
LegendBacon
the error in :
for (int i = 0; i < 3; i++)
{
int index = (int)_saw[1, i];
I already used debug logs but nothing happens :'( can you share me an example how to use list and array ? and I used sqlite to get data.
In this for loop you use "3" as a hard-coded number, but are you sure that "_saw" always has that many at least? For example, an index out of range exception would happen if you tried to access the third element when there are only two.
As for Lists, This looks like a pretty good tutorial. You might also want to take a look at This, as you are using two-dimensional arrays here.
I hope that helps!
~Cheers,
LegendBacon
Answer by W01Fi3 · Jan 13, 2018 at 07:09 AM
As far as I can tell, the problem should be that there just isn't enough elements within the array.
for (int i = 0; i < 3; i++)
When you try to access say an array with only two elements in it, so
AnArray[3] = code;
It will return the error "Array index is out of range".
Basically, make sure when you're using arrays, make sure the index actually has an element.
Also, the first element starts with the index of 0, then element 2 has index of 1, and so on.
Hope this helps in any way
how about I want to use 3 rows only from list to show? my list<> is :
private List<$$anonymous$$ylist> $$anonymous$$yList = new List<$$anonymous$$ylist>();
I want to use sqlite. should I coding this script into :
using (IDataReader reader = dbCmd.ExecuteReader())
{
while (reader.Read())
{
**THE SCRIPTS**
}
}
?