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.