You get an index out of range error when you try to access an array element which doesn't exist.
In C#, arrays are zero based, so an array of
N
elements with have valid indexes between
0
and
(N - 1)
only: an array of three elements such as this:
int[] data = new int[] {1, 2, 3};
Wil have valid indexes of
0
,
1
, and
2
only. And index which is negative or greater than 2 will give you the error.
Since a
List<T>
is built on an array of
T
values, this applies to them as well.
Most likely, it's this line that throws the exception:
structureLevels[currentLevel].SetActive(true);
Because although you check for an empty list above it, you execute the line of code anyway.
Put a breakpoint on that line, and look at the value of
currentLevel
and the content of
structureLevels
to see what exactly you are dealing with: either
currentLevel
is too large, or
structureLevels
is empty. Since you do not assign a new
List<GameObject>
to
structureLevels
in that code we can have no idea how many elements it might contain!