Unity freeze, when trying to compile project with this code

Hi!

I would like to make a simple cubes, containing cubes made from primitive 3D Cube objects. Firstly, I would like to instantiate the core cube, then the cubes near it, then the cubes on the sides of them them, and so on. Howerver, when I would like to run my code, Unity freeze immediately for some reason.

Here is the code:

public class CubeGenerator : MonoBehaviour {
    public GameObject CoreCube;
    public List<GameObject> generatedCubes;

    public int numberOfRows;

    // Use this for initialization
    void Start () {
        GameObject coreCube = Instantiate(CoreCube);
        generatedCubes.Add(coreCube);
        Vector3 currentCenter = coreCube.transform.position;

        double currentPosX = coreCube.transform.position.x;

        Debug.Log("LowerBound: " + Mathf.Floor(numberOfRows / 2));

        for (int i = 0; i < Mathf.Floor(numberOfRows / 2); i++) {
            GameObject currentCube = (GameObject)Instantiate(coreCube, new Vector3(currentCenter.x + 1f, currentCenter.y, currentCenter.z), Quaternion.identity);
            generatedCubes.Add(currentCube);
            Vector3 currentCubePos = currentCube.transform.position;
            currentCenter = currentCubePos;

        }
        currentCenter = coreCube.transform.position;
        for (int i = 0; i < Mathf.Floor(numberOfRows / 2); i++) {
            GameObject currentCube = (GameObject) Instantiate(coreCube, new Vector3(currentCenter.x - 1f, currentCenter.y, currentCenter.z), Quaternion.identity);
            generatedCubes.Add(currentCube);
            Vector3 currentCubePos = currentCube.transform.position;
            currentCenter = currentCubePos;
        }

  
        for (int i = 0; i < generatedCubes.Count; i++) {
            Vector3 currentCenterOfCenterCube = generatedCubes[i].transform.position;
            for (int j = 0; j < Mathf.Floor(numberOfRows / 2); j++) {
                if (j == 0) {
                    currentCenter = currentCenterOfCenterCube;
                    GameObject currentcube = (GameObject) Instantiate(coreCube, new Vector3(currentCenter.x, currentCenter.y, currentCenter.z + 1), Quaternion.identity);
                    generatedCubes.Add(currentcube);
                    currentCenter = currentcube.transform.position;
                }
                else {
                    GameObject currentcube = (GameObject) Instantiate(coreCube, new Vector3(currentCenter.x, currentCenter.y, currentCenter.z + 1), Quaternion.identity);
                    generatedCubes.Add(currentcube);
                    currentCenter = currentcube.transform.position;
                }
            }
            for (int j = 0; j < Mathf.Floor(numberOfRows / 2); j++) {
                if (j == 0) {
                    currentCenter = currentCenterOfCenterCube;
                    GameObject currentcube = (GameObject) Instantiate(coreCube, new Vector3(currentCenter.x, currentCenter.y, currentCenter.z - 1 ), Quaternion.identity);
                    generatedCubes.Add(currentcube);
                    currentCenter = currentcube.transform.position;
                }
                else {
                    GameObject currentcube = (GameObject) Instantiate(coreCube, new Vector3(currentCenter.x, currentCenter.y, currentCenter.z - 1), Quaternion.identity);
                    generatedCubes.Add(currentcube);
                    currentCenter = currentcube.transform.position;
                }
              
            }
       }
    }

The problem seems to be with the

for (int i = 0; i < generatedCubes.Count; i++) {
    ...
}

as when I comment it out and leave just the inner parts of it, it is working properly.
What can be the problem? Why is it freeze Unity everytime?

Thanks in advance!

EDIT: Trying out with

for (int i = 0; i < 5; i++) {
    ...
}

is working properly, so something is not how it should be while I would like to see the arrayList’s size.

You have an infinite loop because the “for loop” repeats for “i < generatedCubes.Count” and you keep adding more items to the list inside it via “generatedCubes.Add(currentCube)”. So after each loop, you’ve even more items to go through in the list and it keeps going forever.

Uh, what a stupid mistake… thank you! :slight_smile: