0

hi guys i'm making a cube swapping game for my project at uni and i have a problem where my code tries to access an array address that doesnt exist, though im not sure how its accessing it. for some reason on the second or third pass it sets l,m and n to 5 and as my array is only 5x5x5 units long it cant find anything.

public void matchCheck()
{ 
      int l;
      int m;
      int n;
    //sweep x axis for matches

   for (l = 0; l < 5; l++)     // the letter k is unclean and must be purged
    {
        for (m = 0; m < 5; m++)
        {
            for (n = 0; n < 5; n++)
            {

                if (n > 0)
                {
                    if (grid[n - 1, m, l] == grid[n, m, l] && xcombo < 6)
                    {

                        xcombo += 1;

                    }
                    else
                    {

                        switch (xcombo)
                        {
                            case 4:
                                Debug.Log("match 5" + cube[n - 1, m, l] + cube[n - 2, m, l] + cube[n - 3, m, l] + cube[n - 4, m, l] + cube[n - 5, m, l]);
                                   grid[n - 1, m, l] = randomNumber();
                                   grid[n - 2, m, l] = randomNumber();
                                   grid[n - 3, m, l] = randomNumber();
                                   grid[n - 4, m, l] = randomNumber();
                                   grid[n - 5, m, l] = randomNumber();
                                   xcombo = 0;
                                   refreshGrid();

                                break;
                            case 3:
                                Debug.Log("match 4" + cube[n - 1, j, l] + cube[n - 2, j, l] + cube[n - 3, j, l] + cube[n - 4, j, l]);
                                 grid[n - 1, m, l] = randomNumber();
                                 grid[n - 2, m, l] = randomNumber();
                                 grid[n - 3, m, l] = randomNumber();
                                 grid[n - 4, m, l] = randomNumber();

                                xcombo = 0;
                                refreshGrid();

                                break;
                            case 2:
                                Debug.Log("match 3" + cube[n - 1, j, l] + cube[n - 2, j, l] + cube[n - 3, j, l]);
                                  grid[n - 1, m, l] = randomNumber();
                                  grid[n - 2, m, l] = randomNumber();
                                  grid[n - 3, m, l] = randomNumber();
                                xcombo = 0;
                                refreshGrid();
                                break;
                        }
                        Debug.Log("combo" + xcombo);
                        xcombo = 0;
                        refreshGrid();

                    }

                }

            }
            n = 0;
        }
        m = 0;
        xcombo = 0;
    }

}
5
  • So you determined from logging/debugging that all three variables are being set to 5 at some point? Commented Nov 22, 2016 at 18:23
  • yeah its super wierd, i was using another 3 called i,j and k that i was using for the for loops in other methods and thought they were getting messed up somewhere else but l,m and n are getting set to 5 and theyre local to matchcheck() Commented Nov 22, 2016 at 18:32
  • it gives this error IndexOutOfRangeException: Array index is out of range. (wrapper managed-to-managed) object:ElementAddr_3_8 (object,int,int,int) Commented Nov 22, 2016 at 19:11
  • Does it consistently give the same line when that error is thrown? Also, what is refreshGrid() doing? Commented Nov 22, 2016 at 19:16
  • Which line is it actually? The first occurence of grid[...] in the respective cycle? Commented Nov 22, 2016 at 19:27

1 Answer 1

1
grid[n - 5

That and all the similar lines (except n-1) will result in negative numbers when n is low.

Also, your for loop starts n at 0, but you immediately check if it is greater than 0. Why not just start it at 1?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.