- Home /
Nested for-loop freezing Unity
for (int i = 0; i < Chunk.Width; i++)
{
for (int j = 0; j < Chunk.Height; j++)
{
for (int k = 0; k < Chunk.Length; k++)
{
//Debug.Log(new Vector3(i, j, k));
//if (random.Next(2) == 1)
//{
// Debug.Log("Adding block");
// chunk.Set(i, j, k, testBlock);
//}
}
}
}
Above code works well. However, the editor freezes on play when I uncomment ANY of the lines within the innermost for-loop.
It's weird that Unity has no problem running the loop when there is nothing in it, but freezes just by adding a Debug.Log line.
I can't seem to figure out what is the problem, so I would be grateful if anyone can tell me what's wrong with the code and how I can fix it.
Answer by Tomer-Barkan · Nov 04, 2013 at 06:58 AM
It's very likely that it simply takes very very long for the for loop to run. It take Chunk.Width
Chunk.Height
Chunk.Length
iterations to run the loop, now if those numbers are large, it could cause unity to seem frozen while it's running those iterations. Now, Debug.Log
is an IO operation which is relatively expensive, so running it 500 x 500 x 500 times will take a long time. And if you do this every frame, then that makes it even worse...
The reason that this doesn't happen when you have no commands inside the for
is simple. The compiler recognizes that nothing whatsoever happens in the for, so it optimizes it by removing the code completely.
If I'm right, first thing you'd want to do is remove the logs because they are a heavy operation. If it still feels stuck, then your design has a performance flaw. Either make the loops smaller, or do them only once in the Start()
method, and simply wait for them to finish.
First of all, thank you for explaining about the optimization.
Another question though: The values for the three dimensions are 32 each and this is only done once in Start() function; is it still too intensive task to completely freeze Unity?
Think of it this way. Debug.Log takes a fairly long time. If each of the dimensions is 32, then with those two log statements, in the worst case you're doing 32*32*32*2 (for both log statements), which is 65536 log statements. That's a lot. If you're printing more than a few hundred at once, you'll have some problems. Take the log statements out, then give it a try.
Like @Hoeloe said, although 32*32*32 = 32$$anonymous$$ is not that bad a thing to do in the Start() method when needed, Debug.Log()
is expensive, so remove them first.
BTW, if you have the script attached to more than one object, than that also multiplies the number of times the for loop runs.
Actually, the original code didn't log. I added later on in the futile tries to figure out what's wrong. I just tested again without the log but with no avail.
On the other hand, I did test after decreasing the size of the dimensions to 1 and 4. And its working so number of iteration being the issue is confirmed, but I don't see why it can't do the following 32 x 32 x 32 times:
public void Set(int x, int y, int z, Block block)
{
blockArray[x, y, z] = block;
Dirty = true;
}
PS. I'm working with one single game object holding the script.
What is Block? Is it efficient?
If you want to test something out without the compiler optimizing, do something like this (note I changed the random to use Random.Range which is the one I'm familiar with, try that too...):
int aaa = 0;
for (int i = 0; i < Chunk.Width; i++)
{
for (int j = 0; j < Chunk.Height; j++)
{
for (int k = 0; k < Chunk.Length; k++)
{
if (Random.Range(0, 3) == 1)
{
aaa++;
}
}
}
}
Your answer
Follow this Question
Related Questions
Nested for-loop freezing Unity 0 Answers
Game random freeze / hang up / stuck 0 Answers
while loops infinitely, I can't tell why. 1 Answer
Determining groups in a 2D array by checking neighbours (and their neighbours etc) 2 Answers
Help! Unsaved work stuck in frozen unity, believed to be from infinite loop. What can i do? 2 Answers