Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Index was out of range error... been stuck for days

Discussion in 'Scripting' started by AcidicDuck, Dec 19, 2020.

  1. AcidicDuck

    AcidicDuck

    Joined:
    Jul 3, 2020
    Posts:
    1
    To start of, here is the generation script that is causing the problems: https://pastebin.com/ELfrQGAX

    The main error is this:
    ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
    System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <fb001e01371b4adca20013e0ac763896>:0)
    System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <fb001e01371b4adca20013e0ac763896>:0)
    LevelGenerator.Start () (at Assets/Scripts/LevelGenerator.cs:137)

    It highlights the line in the script that just simply holds the closing bracket of the Start() function and I can't quite figure out what the issue is other than that!!
    I don't think anything immediately jumps out in terms of what the problem is as the only array that's used in the generation doesn't seem to be called before the problem happens, but it's also all fine in the inspector with no visual errors!
    I've been on this for days now just trying to work around it and just need to get it fixed now and have no idea where to start!
    Even if something doesn't jump out at you for where the problem is, even tips on how to actually find the error (through debug.logs or something would be a massive help)
    Focusing on this problem, and this problem alone today so should be able to reply pretty quickly if you need to ask anything!
     
    Last edited: Dec 19, 2020
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    I don't know what causes the error because I don't know what your fields are set to in the inspector. But for example this looks fishy:
    Code (csharp):
    1.  
    2.             int shopSelection = Random.Range(minDistanceToShop, maxDistanceToShop + 1);
    3.             shopRoom = layoutRoomObjects[shopSelection];
    4.  
    You don't ensure that shopSelection is within the array/list bounds. Same for chestRoom and so on.

    Just search your script for the open bracket "[". Where you find it you are acessing one of the objects of the array/list. Simply put a debug log above that line. This should contain the length of the list/array and the index you want to access it with. And an index should never be smaller than zero! This way you should find any indices which are out of the arrays/lists bounds. So your example would look like:
    Code (csharp):
    1.  
    2.    int shopSelection = Random.Range(minDistanceToShop, maxDistanceToShop + 1);
    3.    Debug.Log(shopSelection + " <= " layoutRoomObjects.length + " ???");
    4.    shopRoom = layoutRoomObjects[shopSelection];
    5.  
    Usually you should ALWAYS check wether a random/calculated index is indeed within the bounds. Especially when the variables can be set from the Editor like in your example (min + maxDistanceToShop).
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Everything that @exiguous says is right on, and I will add this standard blurb:

    Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

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

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I do think this is a bug with Unity. Seems to only ever happen with IndexOutOfRange exceptions.
    Typically, Unity should direct you to the line where the exception was thrown.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Yeah, and it has been around for half a decade now, perhaps forever. Same thing goes for bad key in Dictionary thingies.

    The only solution is to look at the stack track (single-click the log output and look in the lower console frame) and figure it out that way, usually by reading line numbers.