How can I fix UnityEngine.Random.Range (Int32 min, Int32 max) Stack Overflow
This method randomly generates coordinates for a room on a grid for a game I am making:
// Generates a random room anchor point and then dimesions and calculates the vertices
private int[] GenerateRoom()
{
if (roomGenCounter < 5000)
{
// Anchor points
int xAnchor = Random.Range(1, dungeonSize - 1);
int yAnchor = Random.Range(1, dungeonSize - 1);
// Dimensions
int xSize = Random.Range(avgRoomSize - roomSizeDeviation, avgRoomSize + roomSizeDeviation);
int ySize = Random.Range(avgRoomSize - roomSizeDeviation, avgRoomSize + roomSizeDeviation);
// Whether the direction of the width/height is "positive" or "negative"
bool xDir = (Random.value > 0.5f); // This returns either true or false
bool yDir = (Random.value > 0.5f);
// Working out the vertices (top right and bottom left most point) of the room
int[] roomVerts = new int[] { xAnchor, yAnchor, 0, 0 };
if (xDir)
{
roomVerts[2] = xAnchor + xSize;
}
else
{
roomVerts[2] = roomVerts[0];
roomVerts[0] = xAnchor - xSize;
}
if (yDir)
{
roomVerts[3] = yAnchor + ySize;
}
else
{
roomVerts[3] = roomVerts[1];
roomVerts[1] = yAnchor - ySize;
}
// Vertices are stored: { bottom left x, bottom left y, top right x, top right y }
// Checking if the room is off the grid or overlaping a pre-existing room
foreach (int coordinate in roomVerts)
{
if (coordinate < 1 || coordinate > dungeonSize - 1)
{
return GenerateRoom();
}
}
if (RoomOverlap(roomVerts))
{
return GenerateRoom();
}
return roomVerts;
}
else
{
return testArray;
}
}
The stack overflow error:
StackOverflowException UnityEngine.Random.Range (Int32 min, Int32 max) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/RandomBindings.gen.cs:63) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:130) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:166) DungeonGenerator.GenerateRoom () (at Assets/Scripts/DungeonGenerator.cs:171) DungeonGenerator.GenerateRoom () (at Assets/S
The line causeing the error seems to be:
int xAnchor = Random.Range(1, dungeonSize - 1);
however i have tried removing the random part and putting an integer and the error is still the same???
The problems does not come from the line you mentioned but because you call GenerateRoom
badly inside GenerateRoom
itself.