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

To start of, here is the generation script that is causing the problems: using System.Collections;using System.Collections.Generic;using UnityEngine; - Pastebin.com

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 :0)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at :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!

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:

            int shopSelection = Random.Range(minDistanceToShop, maxDistanceToShop + 1);
            shopRoom = layoutRoomObjects[shopSelection];

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:

   int shopSelection = Random.Range(minDistanceToShop, maxDistanceToShop + 1);
   Debug.Log(shopSelection + " <= " layoutRoomObjects.length + " ???");
   shopRoom = layoutRoomObjects[shopSelection];

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).

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

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.

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.