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. Dismiss Notice

Index out of bound, but I dont see the problem

Discussion in 'Scripting' started by DanWef, Nov 23, 2020.

  1. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    Hi! I alway get a index out of bound exception with my code. In unity the index of an array starts with 0 right? array.length returns the amounet of values an array can store right? What is wrong here?



    Code (CSharp):
    1. public bool [,] gameGrid = new bool [5,10];
    2.     public bool [] visitedLocations = new bool [10];
    3.  
    4.  
    5. for (int i=0; i <= 9; i++)
    6.         {
    7.             visitedLocations[i] = false;
    8.  
    9.             for (int k = 0; k <= 4; k++)
    10.             {
    11.                 gameGrid[k, i] = false;
    12.             }
    13.         }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    2 things:

    What does your object's inspector look like? If this is on a MonoBehaviour, then whatever the inspector shows will overwrite your defaults that you have on lines 1 and 2 here. I would guess the array is not as long as you have on your default value.

    Second, there's no reason to hardcode the lengths in your for loop. Do this:

    Code (CSharp):
    1. for (int i=0; i <= visitedLocations.Length; i++)
    2.         {
    3.             visitedLocations[i] = false;
    4.             for (int k = 0; k <= gameGrid.GetLength(0); k++)
    5.             {
    6.                 gameGrid[k, i] = false;
    7.             }
    8.         }
     
  3. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    Looks fine to me.Check which line it is. Maybe it's in a different spot. If it is add a line before the error like: Debug.Log("k="+L+" i="+i);. Maybe it crashes on 0,0, which means the array is totally the wrong size.

    It's also a little micer to use k<5 than k<=4. That way the array size is right there in the line. Later it can become k<A.length when you don't know the size ahead of time.
     
    Joe-Censored likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    Everything Owen and Praetor said above, plus my standard array bounds blurb for you:

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

    http://plbm.com/?p=236

    I'd tack onto Praetor's code above and only do the inner loop
    if (i < grid.GetLength(1)) {
     
    PraetorBlue likes this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,921
    I thought PB was dead-on: public arrays are in the Inspector and are always size 0 unless you type new values into the Inspector. But 2D arrays aren't shown in the Inspector., It's probably safer to follow Unity's guidelines and NEW them in Start or Awake.
     
  6. DanWef

    DanWef

    Joined:
    Jun 4, 2020
    Posts:
    27
    Inspector was the problem. Thank you! :)

    If I do (HideInInspector) does it solve the problem in the future?
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    No but either
    private
    or
    [NonSerialized]
    would solve it.
     
    Bunny83 likes this.