- Home /
C# Array index is out of range but index is zero
public int Laenge = 10;
public int Breite = 10;
public Vector3[] new_Ver = new Vector3[300];
public Vector3[,] new_Ver_s = new Vector3[11,11];
public Vector2[] new_UV = new Vector2[300];
public Vector2[,] new_UV_s = new Vector2[11,11];
void Start () {
int n=0;
for(int l=0;l<=Laenge;l++)
{
for(int b=0;b<=Breite;b++)
{
if(b>1 || l<Laenge)
{
if(b%2 ==1)
{
Debug.Log(l);Debug.Log(b);Debug.Log(n+"f");
new_Ver[n]=new_Ver_s[l,b]; //this is the line where it says theres a bug.
new_UV[n]=new_UV_s[l,b];
n++;
}
}
}
}
It says:
IndexOutOfRangeException: Array index is out of range
but debug says all indizes are zero. Is it because the elements are empty? How can i easily put something in there? If i do the same thing with a twodimensional array, there is no problem. I tried something and replaced new_Uv_s with an empty vector3 variable. It´s still the same error.´
Answer by robertbu · Jun 16, 2013 at 07:23 PM
This code runs without errors for me. Any chance you added the initialization after you attached this script to a game object? Check the size of the arrays in the Inspector. Do a reset (gear icon in the upper right of the component in the inspector).
You are right. Thank you so much. So what do i have to do so it never happens again?
This is a common issue posted to this list. When you make variables public, you need to set their value in the inspector (or do a reset). Or you can make the variables private/protected and set them in code. I still occasionally make this mistake, but burned once I know immediately how to fix it.
You can close this question out by clicking on the checkmark next to the answer. Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104304im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Avatar constructor returns null 1 Answer
Distribute terrain in zones 3 Answers
Getting data from Arraylist within Arraylist 0 Answers
problem with selecting characters script 0 Answers