Hi all,

I’m working on a level generator and, it works exactly how I want it to but it is so very slow it could take up to 10 seconds to generate the map. Is there anywhere that I can speed this script up?

Thanks for looking!

```
var myX : int;
var myZ : int;
var i : int;
var maxTiles : int;
var tileSpin : int;
var tilePrefab : Transform;
var lastTile : Transform;
var lookDirection : int;
var locationVector : Vector3;
static var tiles : Array;
function Awake()
{
tiles = new Array();
locationVector=(Vector3.zero);
tilePrefab = Instantiate(tilePrefab,locationVector, Quaternion.identity);
tiles.push(locationVector);
}
function Update()
{
if (i == maxTiles){Destroy(gameObject,0);}
tileSpin = Random.Range (0,4);
lookDirection = Random.Range(0,5);
Builder();
}
function Builder ()
{
switch (tileSpin) //rotate the cubes at the right angles
{
case (1): tileSpin = 90; break;
case (2): tileSpin = 180; break;
case (3): tileSpin = 270; break;
}
switch (lookDirection) // 4 primaries, I used to have all 8 but it generated ugly and got stuck a lot
{
case (1): locationVector = Vector3(myX, 0,myZ+1);break;
case (2): locationVector = Vector3(myX+1,0,myZ) ;break;
case (3): locationVector = Vector3(myX, 0,myZ-1);break;
case (4): locationVector = Vector3(myX-1,0,myZ) ;break;
}
for (var a = 0; a < tiles.length; a++) // I bet it's in this loop here cause of cycling the array everytime
{
if (locationVector == tiles[a]) // but then... how to accomplish the check?
{
myX = locationVector.x;
myZ = locationVector.z;
return;
}
}
if (i==maxTiles-1)
{
lastTile = Instantiate (lastTile,locationVector, Quaternion.identity);
tiles.push(locationVector);
i++;
return;
}
tilePrefab = Instantiate (tilePrefab,locationVector, Quaternion.Euler(0, tileSpin, 0));
myX = locationVector.x;
myZ = locationVector.z;
tiles.push(locationVector);
i++;
}
```