Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here is some code. It's not all but I think its the important code:

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++;
                }
            }
        }
    }
}

This is quite much code. The if Bracket where the comment is, is where it first runs trough but it stops in the line where the comment is. All debug.logs are zero. If some parts about the code are unclear just ask in the comments.

The question is why does it give me the error message. If I do something similar with a two dimensional array there is no problem. Is it because the elements are empty?

Full error message:

IndexOutOfRangeException: Array index is out of range. meshmut.Start () (at Assets/meshmut.cs:88)

Ok solved. The code is right. Unity3d did something wrong and i had to reset some stuff. Thank you all for showing some other mistakes.

share|improve this question
1  
I suggest setting a breakpoint and stepping through the code using the debugger. –  DuckMaestro Jun 16 '13 at 19:01
5  
You really need refactoring... –  Schaliasos Jun 16 '13 at 19:03
 
i stepped trough the code. n b and l are zero and it gives the error –  Ponato Jun 16 '13 at 19:12
 
Please provide full exception message –  Peuczyński Jun 16 '13 at 19:13
add comment

2 Answers

up vote 2 down vote accepted

To be honest I didn't fully understand your code sample, but in the following IF-block:

if(b>1 || l<Laenge)

b can still be 0 because it's an OR statement, so later inside this IF-block the statements

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

will try to index at -1.

share|improve this answer
 
Thank you. THat´s right i will change that later. –  Ponato Jun 16 '13 at 19:23
1  
Ok the propblem was something else but do you have an idea what i could do instead of the or-statement so it works? –  Ponato Jun 16 '13 at 19:29
 
I'm glad you could solve your problem. Again, without trying to understand your code an easy fix for this problem could be to have two if statements: if (b>1) for the statements with b-1 and if (l<Laenge) for the statements where you index at l+1. But debugging is part of coding. Step through your code with a debugger and try to understand where it goes wrong. –  Chrigi Jun 16 '13 at 19:36
add comment

The problem is clearly that n is being incremented past 300.

You have a pair of nested loops, each of which is defined to run 11 iterations, giving a total of 121 iterations.

Within the loops, you are incrementing n 3 times within the if(l<Laenge || b<Breite) block, plus another 3 times within the if(b>1 || l<Laenge) block. The l<Laenge condition will evaluate to true for the first 10 iterations of the outer loop, meaning that n will get incremented a total of at least 600 times, which is greater than the length of your new_Ver array.

share|improve this answer
 
Thank you i will change that later, but i went step to step trough the code and it gives the error in the first loop when n is zero. –  Ponato Jun 16 '13 at 19:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.