- Home /
I made a silly mistake.
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;
}
}
}
Answer by Eric5h5 · Aug 01, 2012 at 07:05 AM
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.
Strangely enough the instantiate part of the code isn't causing it to be a performance issue, the for loop is. Can someone tell me what is going on?
you should change your loop code to be : for (var z = 0; z <= zLimit; z++)
just in case you forgot to properly set zlimit in the UI and you end up with the default value (0) that would loop for ever.. ;)
same for ylimit
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Script Efficiency 1 Answer
Mover Player With seesaw 1 Answer
Nicknames in multiplayer 1 Answer
Horizontal Slider issue 0 Answers