3 for loops and a IF crashes when the if is true

So i was writing a automated terrain colorizer when i noticed something strange. in my script i have 3 for loops. the 2 first goes from 0 to 1024 for the x and y values of the terrain. while the last for loop is for every texture i want to paint with. This runs fine. However, when i add a IF- statement underneath and it returns true, Unity freezes and refuses to respond. while the if statement returns false, the program runs fine…

after many attempts i decided to see if i could replicate the problem in a new project. so i wrote this wich also crashes try for yourself (put the script on any gameobject and set the variables to true when you press play) :

using UnityEngine;
using System.Collections;

public class ProblemCodeClass : MonoBehaviour {
    public bool run = true;
    public bool theProblem = true;

	void Update () {
        if (run){
            for(int x = 0; x < 1024; x++) {
                for (int y = 0; y < 1024; y++) {
                    for (int z = 0; z < 4; z++) {

                        Debug.Log("I run this fine");

                        if (theProblem){
                            Debug.Log("Im not getting here....");
                        }
                    }
                }
            }
 run = false;
        }
	}
}

Any ideas why or how this can be fixed?

Well, I did a little test with my old PC (Intel Core i5 2400, 8GB ram, onboard graphics) using the Unity 5.3.5f1 and everything seemed to freeze when I hit Play with “theProblem” setted to true. But then I had an idea! Change your test code to this:

using UnityEngine;
using System.Collections;

public class ProblemCodeClass : MonoBehaviour
{
    public int firstLoop = 4;
    public int secondLoop = 4;
    public int thirdLoop = 4;

    public bool run = true;
    public bool theProblem = true;

    void Update()
    {
        if (run)
        {
            for (int x = 0; x < firstLoop; x++)
            {
                for (int y = 0; y < secondLoop; y++)
                {
                    for (int z = 0; z < thirdLoop; z++)
                    {
                        Debug.Log("I run this fine");

                        if (theProblem)
                        {
                            Debug.Log("Im not getting here....");
                        }
                    }
                }
            }

            run = false;
        }
    }
}

I turned everything to false in inspector, then hit play. It runs.

While playing, setted run to true. It runs with the correct output.

Still while playing, setted theProblemto true, then settedrunto true again. Evertyhing is fine.

If I change firstLoopto 1024, then set everything to true again, the editor freezes for almost 1 minute then shows me the correct output.

If I change secondLoop to 1024 too, then set everything to true again, the editor freezes for some minutes then shows me the correct output.

So, this is not a Unity problem, it just did not finished the task. Debug.Logis somewhat expensive, so wait some time if you have a low profile PC to se if it remais frozen.