script causing unity to crash

With following code unity outright crashes when I try to run the game.

function UncoverNeighbors(x, y) {
	if(x + 1 >= 0  x + 1 < gridX  y >= 0  y < gridY) { 
		cells[(y * gridX) + (x + 1)].GetComponent(cellFunction).Uncover();
	}
	if(x - 1 >= 0  x - 1 < gridX  y + 1 >= 0  y + 1 < gridY) { 
		cells[((y + 1) * gridX) + (x - 1)].GetComponent(cellFunction).Uncover();
	}
	if(x  >= 0  x  < gridX  y + 1 >= 0  y + 1 < gridY) { 
		cells[((y + 1) * gridX) + x ].GetComponent(cellFunction).Uncover();
	}
	if((x + 1) >= 0  (x + 1) < gridX  (y + 1) >= 0  (y + 1) < gridY) { 
		cells[((y + 1) * gridX) + (x + 1)].GetComponent(cellFunction).Uncover();
	}
	if(x - 1 >= 0  x - 1 < gridX  y - 1 >= 0  y - 1 < gridY) { 
		cells[((y - 1) * gridX) + (x - 1)].GetComponent(cellFunction).Uncover();
	}
	if(x >= 0  x < gridX  y - 1 >= 0  y - 1 < gridY) { 
		cells[((y - 1) * gridX) + x].GetComponent(cellFunction).Uncover();
	}
	if(x + 1 >= 0  x + 1 < gridX  y - 1 >= 0  y - 1 < gridY) { 
		cells[((y - 1) * gridX) + (x + 1)].GetComponent(cellFunction).Uncover();
	}
	if(x - 1 >= 0  x - 1 < gridX  y >= 0  y < gridY) { 
		cells[(y * gridX) + (x - 1)].GetComponent(cellFunction).Uncover();
	}
}

However, if I comment out the bottom 4 ifs, it runs fine. If I comment out the top 4 ifs, it runs fine. But when they are all run together I get a crash.

For example

//Runs fine
function UncoverNeighbors(x, y) {
	if(x + 1 >= 0  x + 1 < gridX  y >= 0  y < gridY) { 
		cells[(y * gridX) + (x + 1)].GetComponent(cellFunction).Uncover();
	}
	if(x - 1 >= 0  x - 1 < gridX  y + 1 >= 0  y + 1 < gridY) { 
		cells[((y + 1) * gridX) + (x - 1)].GetComponent(cellFunction).Uncover();
	}
	if(x  >= 0  x  < gridX  y + 1 >= 0  y + 1 < gridY) { 
		cells[((y + 1) * gridX) + x ].GetComponent(cellFunction).Uncover();
	}
	if((x + 1) >= 0  (x + 1) < gridX  (y + 1) >= 0  (y + 1) < gridY) { 
		cells[((y + 1) * gridX) + (x + 1)].GetComponent(cellFunction).Uncover();
	}
}


//Also would run fine
function UncoverNeighbors(x, y) {
	if(x - 1 >= 0  x - 1 < gridX  y - 1 >= 0  y - 1 < gridY) { 
		cells[((y - 1) * gridX) + (x - 1)].GetComponent(cellFunction).Uncover();
	}
	if(x >= 0  x < gridX  y - 1 >= 0  y - 1 < gridY) { 
		cells[((y - 1) * gridX) + x].GetComponent(cellFunction).Uncover();
	}
	if(x + 1 >= 0  x + 1 < gridX  y - 1 >= 0  y - 1 < gridY) { 
		cells[((y - 1) * gridX) + (x + 1)].GetComponent(cellFunction).Uncover();
	}
	if(x - 1 >= 0  x - 1 < gridX  y >= 0  y < gridY) { 
		cells[(y * gridX) + (x - 1)].GetComponent(cellFunction).Uncover();
	}
}

For reference, this is what Uncover is

function Uncover() {
	if(uncovered == 0) {
		if(bomb){
				var particleEffect = Instantiate(particleEffect, transform.position, Quaternion.identity);
				Camera.main.GetComponent(CreateGrid).lose = 1;
				yield new WaitForSeconds (3);
				Application.LoadLevel ("level1");
			}
		else {
			if(surroundingBombs == 0) {
				renderer.material = dig0;
				Camera.main.GetComponent(CreateGrid).UncoverNeighbors(xLoc, yLoc);
			}
			if(surroundingBombs == 1) renderer.material = dig1;
			if(surroundingBombs == 2) renderer.material = dig2;
			if(surroundingBombs == 3) renderer.material = dig3;
			if(surroundingBombs == 4) renderer.material = dig4;
			if(surroundingBombs == 5) renderer.material = dig5;
			if(surroundingBombs == 6) renderer.material = dig6;
			if(surroundingBombs == 7) renderer.material = dig7;
			if(surroundingBombs == 8) renderer.material = dig8;
		}
	}
	uncovered = 1;
}

I figured I’d start off easy by making a minesweeper game. Everythings working great except for this bit of code :evil:
Any help would be appreciated.

It seems UncoverNeighbors is calling Uncover, which is calling UncoverNeighbors! Can you say Infinite loop …

Secondly, you are potentially loading a level (“level1”) eight times within a very short period of time.

You’ve a lot of stuff going on: index computations, instantiations, rapid level loading(!?), recursive (infinite loop?) calls, and a yield thrown in for good measure. A lot of complex interactions with a lot of possibilities for things to go fubar. My advice would be to re-think/re-write these two routines …

Thanks for the reply. As far as those two functions go, they’re supposed to be recursive. Essentially what uncover is doing is, if the cell is empty and there are no bombs in the surrounding squares, uncover all the squares around it and for each of those squares that has no bombs in their respective surrounding squares then uncover those. Im only using a small 5x5 grid at the moment so even at worse case the Uncover function would be run 25 times (ie, there are no bombs in the entire grid). As far as the level loading goes, that statement can be ignored since the base cases prevent that case from ever running (and I have removed it from the code). When I run the code that does work even on larger grids (50*50) it runs perfectly fine with no visible latency.

Basically, whats confusing me is when I have up to 8 calls to Uncover in my code Unity crashes. When I randomly comment or delete one or two of those calls everything works.
ie

//This does not work
if((x - 1, y - 1) is valid) uncover that cell;
if((x - 1, y) is valid)     uncover that cell;
if((x - 1, y + 1) is valid) uncover that cell;
if((x, y - 1) is valid)     uncover that cell;
if((x, y + 1) is valid)     uncover that cell;
if((x + 1, y - 1) is valid) uncover that cell;
if((x + 1, y) is valid)     uncover that cell;
if((x + 1, y + 1) is valid) uncover that cell;

//This does work, but doesnt do everything I need
if((x - 1, y - 1) is valid)   uncover that cell;
//if((x - 1, y) is valid)     uncover that cell;
if((x - 1, y + 1) is valid)   uncover that cell;
if((x, y - 1) is valid)       uncover that cell;
//if((x, y + 1) is valid)     uncover that cell;
//if((x + 1, y - 1) is valid) uncover that cell;
if((x + 1, y) is valid)       uncover that cell;
if((x + 1, y + 1) is valid)   uncover that cell;

//Or this works too
//if((x - 1, y - 1) is valid) uncover that cell;
if((x - 1, y) is valid)       uncover that cell;
if((x - 1, y + 1) is valid)   uncover that cell;
if((x, y - 1) is valid)       uncover that cell;
if((x, y + 1) is valid)       uncover that cell;
if((x + 1, y - 1) is valid)   uncover that cell;
//if((x + 1, y) is valid)     uncover that cell;
//if((x + 1, y + 1) is valid) uncover that cell;

Eh, probably just sloppy coding somewhere