- Home /
Question by
Robi D · Jul 21, 2014 at 04:18 PM ·
javascriptarray
Unity freezes on play, if a script tries to edit Arrays
I started to make a Minesweeper game for fun, but for some reason, editing arrays doesn't seem to work at all. If I include the code in a script, attach it to an object, then when I click on play, Unity completely freezes and I have to kill the process from the Task Manager.
The code:*
var TotalTiles = 81; // Total number of tiles on the current board
var RandomNumber : int;
var MineArray : int[]; // Array of all mines
var TotalMines = 10; // Total number of mines
var restart = false;
function SetMines()
{
// Set all 10 mines using a loop
for ( var index = 0; index < TotalMines;)
{
restart = false;
// Select a random tile with Random.Range returning a number
// between 1.0 and 81.99 and converting it to an int.
// Since each tile is numbered ( from 1 to 81 ); the Random number
// will be the "ID" of the tile, the current mine will be hidden in.
RandomNumber = Random.Range( 1.0, TotalTiles + 0.99 );
// Check if any of the previous mines has the same ID as the current one,
for ( var i = 0; i < index; )
{
// if there is, reset everything and start the loop again
if ( MineArray[i] == RandomNumber )
{
restart = true;
i = 0;
break;
}
// if there is not, continue until it has checked every mine
else
{
i += 1;
}
}
if ( restart == true )
{
continue;
}
// After all mines are checked, assign the ID to the current mine,
// and store in in MineArray at the current index, then continue the loop
else
{
MineArray[index] = RandomNumber;
if ( index < TotalMines )
{
index += 1;
continue;
}
else
{ break; }
}
}
}
Any ideas why this doesn't work?
Thanks
Comment
I don't see an infinite loop in this code, but the logic is pretty convoluted. As an alternate:
create an int array and initialize the entries from 1 to 81
shuffle the array
use the first 10 entries (or whatever Total$$anonymous$$ines is) to initialize $$anonymous$$ineArray[].
Your answer