Unity Code Performance Issue

The for loop in this code is causing Unity to crash, why would that be? Here’s the code:

var yLimit : int;
var zLimit : int;

var gridStart : Vector3;

var spacing : float = 10;
var gridPos : Transform;

function Start () {

gridStart = transform.position; 

for (var z = 0; z < zLimit; z++) {

for (var y = 0; y < yLimit; y--) {

var newGridPos = Instantiate(gridPos, Vector3 (0, z * spacing, y * spacing), Quaternion.identity);

newGridPos.transform.parent = transform;

}

}

}

You’re creating yLimit * zLimit objects every frame. Naturally this will cause major issues. I would suggest not doing that…

Also, your inner loop is infinite, assuming yLimit is a positive number, which is an even more major problem.