- 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.
This looks to be a duplicate of http://answers.unity3d.com/questions/568762/nested-for-loop-freezing-unity-1.html - could a mod check/remove please?
Is this in an update() function? If so how big is Chunk.Width, Chunk.Height, and Chunk.Length? If this is the case you'll be logging and completing this Set function a great many times per second. That's some extreme processing that will likely freeze your game.
Of course if you leave it empty it's not nearly as much processing and won't freeze.
Yeah, don't know how it got posted again. Can a mod please delete? It is not allowing me to delete it myself...
Your answer
Follow this Question
Related Questions
Nested for-loop freezing Unity 1 Answer
Scripts freezes game for a few seconds after build. 1 Answer
Unity Freezing 1 Answer
Unity Keeps Freezing on Play 5 Answers
Best way to make these tree iterations? Arrays? tags? nested etc 1 Answer