-5

这是一些代码。这不是全部,但我认为它是重要的代码:

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];
public int[] new_Tri =  new int[300];

void Start () { 
int n=0;

    for(int l=0;l<=Laenge;l++)
    {
        int k = 0;
        for(int b=0;b<=Breite;b++)
        {
            Debug.Log(l);Debug.Log(b);Debug.Log(n+"n");

            if(l<Laenge || b<Breite)
            {
                Debug.Log(l);Debug.Log(b);Debug.Log(n+"n");

                if(b%2 == 1)
                {
                    Debug.Log(l);Debug.Log(b);Debug.Log(n+"n");
                    new_Ver[n]=new_Ver_s[l,b];
                    new_UV[n]=new_UV_s[l,b];
                    n++;
                    new_Ver[n]=new_Ver_s[1+l,1+b];
                    new_UV[n]=new_UV_s[1+l,1+b];
                    n++;
                    new_Ver[n]=new_Ver_s[1+l,b];
                    new_UV[n]=new_UV_s[1+l,b];
                    n++;
                }
                else
                {
                    Debug.Log(l);Debug.Log(b);Debug.Log(n+"nl");
                    Vector3 pop = new_Ver_s[l,b];
                    Debug.Log(l);Debug.Log(b);Debug.Log(n);//All debug. logs are zero
                    new_Ver[n] = pop; //This is the line where it gives the error message : Array index is out of range. 
                    new_UV[n]= pop;
                    Debug.Log(l);Debug.Log(b);Debug.Log(n);
                    n++;
                    pop = new_Ver_s[l,1+b];
                    new_Ver[n]=pop;
                    new_UV[n]=pop;
                    n++;
                    pop=new_Ver_s[1+l,b];
                    new_Ver[n]=pop;
                    new_UV[n]=pop;
                    n++;
                    Debug.Log(l);Debug.Log(b);Debug.Log(n+"neo");
                }

                Debug.Log(l);Debug.Log(b);Debug.Log(n+"n");
            }

            if(b>1 || l<Laenge)
            {
                Debug.Log(l);Debug.Log(b);Debug.Log(n+"fn");
                if(b%2 ==1)
                {
                    Debug.Log(l);Debug.Log(b);Debug.Log(n+"f");
                    new_Ver[n]=new_Ver_s[l,b];
                    new_UV[n]=new_UV_s[l,b];
                    n++;
                    new_Ver[n]=new_Ver_s[l+1,b];
                    new_UV[n]=new_UV_s[l+1,b];
                    n++;
                    new_Ver[n]=new_Ver_s[l+1,b-1];
                    new_UV[n]=new_UV_s[l+1,b-1];
                    n++;
                }
                else
                {
                    Debug.Log(l);Debug.Log(b);Debug.Log(n+"f");
                    new_Ver[n]=new_Ver_s[l,b];
                    new_UV[n]=new_UV_s[l,b];
                    n++;
                    new_Ver[n]=new_Ver_s[l+1,b];
                    new_UV[n]=new_UV_s[l+1,b];
                    n++;
                    new_Ver[n]=new_Ver_s[l,b-1];
                    new_UV[n]=new_UV_s[l,b-1];
                    n++;
                }
            }
        }
    }
}

这是相当多的代码。注释所在的 if 括号是它第一次运行的地方,但它在注释所在的行中停止。所有 debug.logs 都为零。如果代码的某些部分不清楚,请在评论中提问。

问题是为什么它会给我错误信息。如果我对二维数组做类似的事情,那就没有问题了。是因为元素是空的吗?

完整的错误信息:

IndexOutOfRangeException:数组索引超出范围。meshmut.Start () (在 Assets/meshmut.cs:88)

好的解决了。代码是对的。Unity3d 做错了什么,我不得不重置一些东西。谢谢大家指出其他一些错误。

4

2 回答 2

2

老实说,我没有完全理解您的代码示例,但在以下 IF 块中:

if(b>1 || l<Laenge)

b 仍然可以为 0,因为它是一个 OR 语句,所以稍后在这个 IF 块中的语句

new_Ver[n]=new_Ver_s[l,b-1];
new_UV[n]=new_UV_s[l,b-1];

将尝试在 -1 处索引。

于 2013-06-16T19:09:09.600 回答
0

问题显然n是增加超过 300。

您有一对嵌套循环,每个循环都定义为运行 11 次迭代,总共进行 121 次迭代。

在循环中,您在块内增加n3 次,在if(l<Laenge || b<Breite)块内再增加 3 次if(b>1 || l<Laenge)。对于外循环的前 10 次迭代,l<Laenge条件将评估true为,这意味着n它将总共增加至少 600 次,这大于new_Ver数组的长度。

于 2013-06-16T19:07:14.910 回答