Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Array out of range

Discussion in 'Scripting' started by phoda, Jul 21, 2015.

  1. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    How can I check if array is out of range.
    i use int[,] and I want to know how to check if lets say variable[11,9] is out of range if i set int[,] to be [10,10]?
    because error stops whole script
     
  2. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    (array).GetLength(dimension);

    So if you want to check if 11,9 is out of range, check if 11 is greater than (array).GetLength(0) and if 9 is greater than (array).GetLength(1)
     
    glatteinfo and eisenpony like this.
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    int[,] is implemented as an array of arrays. For my example, some you initialized an int[,] as
    Code (csharp):
    1. var matrix = new int[,] { {1, 2, 3}, {4, 5}, {6, 7, 8} };
    You can check your first indexer against matrix.length. You'll notice my matrix has different sized sub arrays. This is allowed, which means the second indexer will need to be checked for each sub array.

    matrix.length is 3
    matrix[0].length is 3
    matrix[1].length is 2
    etc...
     
    jchambers likes this.
  4. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    So i should check
    If (x>=0 && x<array.getlength(0))
    Print("inside");

    So in this case 0 checks rows and 1 colums in getlength?
     
  5. HandOfPriam

    HandOfPriam

    Joined:
    Aug 19, 2010
    Posts:
    34
    The array you defined is a 2D array. As such, it represents a table-like set of data (thinks rows and columns). Think of the 2D array as follows: the first index, x, represents the current column, and the second index, y, represents the value within the column. All the above code does is check that the index falls inside of the bounds of the x-index, that is, falls on a valid column. GetLength(0) would be the length of the column, and GetLength(1) would be the length of the row, in this case.
     
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Hmm I must have been thinking of a different language - C# does not allow jagged arrays like I suggested. You can still use the approach I gave but I think I like @RiokuTheSlayer 's GetLength method better.

    You can think of the rows and columns in whichever order you like, though I think the unspoken standard is to list columns first and rows second. Just be sure that you test your first parameter against GetLength(0) and second against GetLength(1). GetLength isn't just for 2D arrays either; it can be used for an array of any rank up to 32 dimensions (though array's are capped at 2GB size, so you'll probably get an out of memory exception pretty quick!)
     
  7. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    i used getlength metod as 0 was describing rows and 1 columns
    thanks for help
     
  8. Oribow

    Oribow

    Joined:
    Nov 9, 2012
    Posts:
    38
    C# does allow jagged Arrays: int[][]
     
    eisenpony likes this.
  9. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    971
    Ahh. I stand corrected.