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

Bug ArgumetnOutOfRangeException: Index was out of range.

Discussion in 'Scripting' started by MyszaPipisza, Jan 10, 2023.

  1. MyszaPipisza

    MyszaPipisza

    Joined:
    Jan 20, 2022
    Posts:
    4
    Hello, i'm new in unity and decide to make "RandomDungeon generator" from this tutorial

    .

    and get the error "ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection Parameter name: index"

    I was checking a code many times but stil don't get it.
    Thanks for help and sorry if I make an language mistake (I'm not the best at eanglish).

    Here's my code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DungeonGenerator : MonoBehaviour
    6. {
    7.     public class Cell
    8.     {
    9.         public bool visited = false;
    10.         public bool[] status = new bool[4];
    11.  
    12.     }
    13.  
    14.     public Vector2 size;
    15.     public int startPos = 0;
    16.     public GameObject room;
    17.     public Vector2 offset;
    18.  
    19.     List<Cell> board;
    20.  
    21.  
    22.  
    23.     void GenerateDungeon()
    24.     {
    25.         for(int i = 0; i < size.x; i++)
    26.         {
    27.             for(int j = 0; j < size.y; j++)
    28.             {
    29.                 var newRoom = Instantiate(room, new Vector3(i*offset.x,0,-j*offset.y), Quaternion.identity, transform).GetComponent<RoomBehaviour>();
    30.                 newRoom.UpdateRoom(board[Mathf.FloorToInt(i+j*size.x)].status);
    31.  
    32.                 newRoom.name += " " + i + "-" + j;
    33.             }
    34.  
    35.  
    36.         }
    37.     }
    38.  
    39.     void Start()
    40.     {
    41.         MazeGenerator();
    42.     }
    43.  
    44.     void MazeGenerator()
    45.     {
    46.         board = new List<Cell>();
    47.         for (int i = 0; i < size.x; i++)
    48.         {
    49.             for (int j = 0; j < size.y; j++)
    50.             {
    51.                 board.Add(new Cell());
    52.             }
    53.         }
    54.         int currentCell = startPos;
    55.  
    56.         Stack<int> path = new Stack<int>();
    57.  
    58.         int k = 0;
    59.  
    60.         while(k < 1000)
    61.         {
    62.             k++;
    63.  
    64.             board[currentCell].visited = true;
    65.  
    66.             List<int> neighbors = CheckingNeighbors(currentCell);
    67.  
    68.             if (neighbors.Count == 0)
    69.             {
    70.                 if(path.Count == 0)
    71.                 {
    72.                     break;
    73.                 }
    74.                 else
    75.                 {
    76.                     currentCell = path.Pop();
    77.                 }
    78.             }
    79.             else
    80.             {
    81.                 path.Push(currentCell);
    82.  
    83.                 int newCell = neighbors[Random.Range (0, neighbors.Count)];
    84.  
    85.                 if(newCell > currentCell)
    86.                 {
    87.                     if(newCell - 1 == currentCell)
    88.                     {
    89.                         board[currentCell].status[2] = true;
    90.                         currentCell = newCell;
    91.                         board[currentCell].status[3] = true;
    92.                     }
    93.                     else
    94.                     {
    95.                         board[currentCell].status[1] = true;
    96.                         currentCell = newCell;
    97.                         board[currentCell].status[0] = true;
    98.                     }
    99.                 }
    100.                 else
    101.                 {
    102.                     if(newCell + 1 == currentCell)
    103.                     {
    104.                         board[currentCell].status[3] = true;
    105.                         currentCell = newCell;
    106.                         board[currentCell].status[2] = true;
    107.                     }
    108.                     else
    109.                     {
    110.                         board[currentCell].status[0] = true;
    111.                         currentCell = newCell;
    112.                         board[currentCell].status[1] = true;
    113.                     }
    114.                 }
    115.             }
    116.         }
    117.         GenerateDungeon();
    118.     }
    119.  
    120.     void Update()
    121.     {
    122.  
    123.     }
    124.  
    125.     List<int> CheckingNeighbors(int cell)
    126.     {
    127.         List<int> neighbors = new List<int>();
    128.  
    129.         if (cell - size.x >= 0 && !board[Mathf.FloorToInt(cell - size.x)].visited)
    130.         {
    131.             neighbors.Add(Mathf.FloorToInt(cell - size.x));
    132.         }
    133.  
    134.                 if (cell + size.x < board.Count && !board[Mathf.FloorToInt(cell + size.x)].visited)
    135.         {
    136.             neighbors.Add(Mathf.FloorToInt(cell + size.x));
    137.         }
    138.  
    139.                         if ((cell+1) % size.x != 0 && !board[Mathf.FloorToInt(cell + size.x)].visited)
    140.         {
    141.             neighbors.Add(Mathf.FloorToInt(cell +1));
    142.         }
    143.      
    144.                                 if (cell % size.x != 0 && !board[Mathf.FloorToInt(cell - size.x)].visited)
    145.         {
    146.             neighbors.Add(Mathf.FloorToInt(cell -1));
    147.         }
    148.  
    149.         return neighbors;
    150.  
    151.     }
    152.  
    153.  
    154.  
    155.  
    156.  
    157. }
    One more time thanks :)
    P.S great tutorial
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Hello, you have omitted some key information from your error message. Every error message contains a filename and line number of the error. Please give the full error message as it shows in your inspector window. It will make it easier to help you.
     
    MyszaPipisza likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

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

    Steps to success:
    - find which collection it is and what line of code accesses it <--- critical first step!)
    - find out why it has fewer items than you expect
    - fix whatever logic is making the indexing value exceed the collection size
    - remember you might have more than one instance of this script in your scene/prefab
    - remember the collection may be used in more than one location in the code
    - remember that indices start at ZERO (0) and go to the count / length minus 1 (three (3) items are 0,1,2 only)
     
    MyszaPipisza likes this.
  4. MyszaPipisza

    MyszaPipisza

    Joined:
    Jan 20, 2022
    Posts:
    4
    Thank you for help! I very apreaciate you spent your time looking at my code :]
     
  5. MyszaPipisza

    MyszaPipisza

    Joined:
    Jan 20, 2022
    Posts:
    4
    Thank you for looking at my code and helping :] Here's full error message

    [19;29;37] ArgumentOutOffRangeExeptation: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name Index

    Oh and here's photo
     
    Last edited: Jan 10, 2023
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,947
    Looking at the above code, it has many places where it blindly accesses collections via a computed index.

    Anywhere you see square brackets such as
    [
    and
    ]
    it is accessing a collection.

    In many places it appears to access these values with very complex expressions, and does not ensure they are between 0 and the size minus one.

    Examples are here:

    These are likely to be problematic.

    It is always better to compute the index value and check it before using it.

    Other places include the two different dereferences here:

    Is currentCell between zero and the board length?

    Is status at least 2 items long?

    etc.
     
    MyszaPipisza likes this.
  7. MyszaPipisza

    MyszaPipisza

    Joined:
    Jan 20, 2022
    Posts:
    4
    Thank you for lots of help! now I'm probably know how it work ;)