Hi, I've got an odd issue with the below code (I've stripped out the parts that are irrelevant, and any classes/functions referenced are working as expected):
int curNumRooms = 0;
while(curNumRooms < numberOfRooms) {
int w = Random.Range(minimumRoomSize, maximumRoomSize+1);
int h = Random.Range(minimumRoomSize, maximumRoomSize+1);
int x = Random.Range(0, (int)levelSize.x - w - 1);
int y = Random.Range(0, (int)levelSize.y - h - 1);
Rectangle newRoom = new Rectangle(x,y,w,h);
bool failed = false;
foreach (Rectangle otherRoom in rooms) {
if(otherRoom != null) {
if (newRoom.Intersect(otherRoom)) {
failed = true;
break;
}
}
}
if (!failed) {
rooms[curNumRooms] = newRoom;
curNumRooms++;
}
}
For some reason, `failed` always evaluates to true. I threw in a couple debug messages, and for some reason, failed evaluates twice -- the first time, in the foreach loop, it evaluates correctly. The second time, it evaluates to false. If I initialize `failed` as true, then it evaluates to true the second time, almost as if the while loop was being run twice, and ignoring the foreach loop the second time around.
Why is this?