Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I created a script that used to check all number at mesh.triangles and save the value if we did not save it yet to array variable that i created manually in script , but before the script even go check , it's stuck at comparing normal variable with array variable , but why ? because we can't compare normal variable with array variable or what ? here is my script :

Mesh secondmesh = GetComponent<MeshFilter> ().mesh;     
Vector3[] secondvertices = secondmesh.vertices;    
int[] secondtriangle = secondmesh.triangles;   
int saverone = one;
int savertwo = two;   
int saverthree = three;
int[] saveint = new int[secondtriangle.Length];
Vector3[] savevector = secondvertices;

// check for number that available in triangle
for (int anythingtwo = 1; anythingtwo < ( secondvertices.Length + 1 ) ; anythingtwo++)
{
    saveint [0] = 0;
    int idontknowthistriangle = secondmesh.triangles [anythingtwo];
    bool isalreadyavailableinsaveint = false;

    // check if the same number already exist in the array 
    for(int anythingthree = 0; anythingthree < ( saveint.Length + 1) 
        && anythingthree < ( secondtriangle.Length + 1) ;anythingthree++) 
    {
        // the error is at line below this comment and another line 
        // that is in the bottom of the script
        if (idontknowthistriangle == saveint [anythingthree]) 
        {
            Debug.Log (" | anything three , already found ! |");
            isalreadyavailableinsaveint = true;
            break;
        }
    }

    // if the same number is found in the array
    if (isalreadyavailableinsaveint == true) 
    {
        Debug.Log (" | already available in save int ! | ");
    }

    // if the array does not contain same number 
    if (isalreadyavailableinsaveint == false) 
    {
        // count how many times the search found that the array 
        // don't contain the value we found
        totalcount++;
        Debug.Log (" | total count : " + totalcount + " | ");

        // error also here ! 
        saveint [anythingtwo] = idontknowthistriangle;
    }
}            

This is the error immediately :

// the variable used at the error    
int[] secondtriangle = secondmesh.triangles;
int[] saveint = new int[secondtriangle.Length];
int idontknowthistriangle = secondmesh.triangles [anythingtwo];
// the error is located at 
if (isalreadyavailableinsaveint == true) {  
// and  
saveint [anythingtwo] = idontknowthistriangle;

The error is located using the comment at the script , but i don't get it why , can anyone tell me why is this happening ? because what i though int never be null and i can even debug log the variable ( output 0 ) no matter i set it or not .

share|improve this question
    
"For those lazy that want to know where is the error immediately : " That's a really condescending way of phrasing it, especially since that code snippet does not provide enough information anyway. Also it's quite obvious that you are accessing the array out of bounds: anythingtwo is not related to the saveint array in any way and you don't check its bounds. And anythingthree < ( saveint.Length + 1) is already defined to go out of bounds – UnholySheep Mar 14 at 8:35
    
And you should really learn to give your variables descriptive names - makes code way easier to read and understand – UnholySheep Mar 14 at 8:37
    
Niote that I have removed the condescending lines (in favour of flagging this as a breach of our "be nice" policy), and applied correct indentation to code so I could read through it more easily. I an interpret that there is a null reference exception, but the grammar is much to be desired, and I am worried any further edits may deviate from your intent. – Gnemlock Mar 14 at 8:50
    
@Gnemlock I'm interpreting the question title as OP receiving an IndexOutOfRangeException (which would be consistent with my analysis of the code) - not sure why OP assumes that there are null values in the arrays – UnholySheep Mar 14 at 8:52
    
Also note that we expect a degree of research effort. This is the second question in less than a day asking about a trivial problem, given an understanding of arrays in C#. It is feasible that you may miss things from time to time, but without showing research effort, users will seldom give you the benefit of the doubt. In case you run in to any more trouble using arrays, I have linked some documentation and tutorials. Favorite them. Read through them. Read through the other entries. You will run into these concepts sooner or later, in one way or another. – Gnemlock Mar 14 at 9:17

Reading through your code, it is fairly obvious that you have logic errors that would lead to null reference exceptions. Here are some excerpts, with appropriate formatting:

// We create an array of integers, with the same length as the secondTriangle array.
int[] saveInt = new int[secondTriangle.Length];

// We than create a for loop, which iterates using anythingThree.
// Explicitly note that this for loop exits when anythingThree is no longer less than both 
// the lengths of saveInt and secondTriangle, + 1.
for(int anythingThree = 0; anythingThree < (saveInt.Length + 1) 
    && anythingThree < (secondTriangle.Length + 1); anythingThree ++)
{
    // Lastly, we perform an if statement, checking the values in saveInt
    if (iDontKnowThisTriangle == saveInt [anythingThree])
    {
    }
}

In short, you are creating an array called saveInt with the same length as secondTriangle. You are then entering a for loop where you iterate between 0 and saveInt.Length or secondTriangle.Length 1. You use the current value to check the saveInt[] array, so naturally, as soon as you hit saveInt.Length, you are accessing an index that does not exist in the array.

Why is that?

Simply put, saveInt.Length or secondTriangle.Length will return the actual length of the arrays. However, in this format, we count the first index as 1 and count up. This is a kin to how we count, in the real world. However, the first index of an array is not 1. It is 0. So the value you derive as the Length value of an array will always point to the first index that is not available.

Perhaps you should be ensuring that your for loop uses the condition of < saveInt.Length. This ensures that you work within the bounds of your array, as well as removing the redundancy caused by including a check to the secondTriangle array, despite already ensuring that these arrays have the same length.


Further consideration

Only yesterday, you asked another question that was actually quite obvious given an understanding of how arrays work in C#. It is important to get the basics down before we move on to more advanced topics, such as those more commonly found in game development.

It is definitely worth doing some research on how arrays work in C#, as they can be quite tricky, if you are unfamiliar with their operation. You might want to consider reviewing the following documents:


1 Note that you explicitly ensure that these two arrays have the same length. Checking that the value is both < (saveInt.Length + 1) and < (secondTriangle.Length + 1) is also rather redundant. We know that if x < saveInt.Length then x < secondTriangle.Length, because they have the same lengths.

share|improve this answer

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.