Infinite For Loops? (C#) [ANSWERED]

So what I’m trying to do here is make a floor out of tiles (there’s a reason for that that’s unrelated to the issue) so I have a prefab I dropped in and then I’m trying instantiate 100 of them. What happens when I click play is that it just never loads and I have to force quit the program. I know it CAN instantiate 100 because I’ve done that and had no issues, it just put them in the wrong spots so I had to change the code around. It’s not a hardware issue that it’s just taking a long time.

I’m sure I just messed up the logic of the for loops or some amateur mistake so I figured I’d drop this in here and see if some more experienced programmers could point out what I did wrong.

Thanks in advance for your help!

using UnityEngine;
using System.Collections;

public class DiscoFloor : MonoBehaviour {

	public Transform floorTile;

	private float xMin = -450.0f;
	private float xMax = 450.0f;
	private float zMin = -450.0f;
	private float zMax = 450.0f;
	private int i;
	private int iTwo;
	private Vector3[] placementArr;



	void Start() {

		placementArr = new Vector3[100];
		//placementArr [0] = new Vector3 (-450.0f, 0, 450.0f);
		//placementArr [100] = new Vector3 (450.0f, 0, -450.0f);

		for (int i = 0; i < placementArr.Length; i++){

			for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f){

				for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f){

					placementArr *= new Vector3 (xMin, 0, zMin);*
  •  			for (int iTwo = 0; iTwo < placementArr.Length; iTwo++) {*
    
  •  				Instantiate(floorTile, placementArr[iTwo], Quaternion.identity);*
    
  •  			}*
    
  •  		}*
    
  •  	}*
    
  •  }*
    
  • }*

  • // Update is called once per frame*

  • void Update () {*

  • }*
    }

Your loops don’t make much sense to me. You have 4 nested loops and two are iterating over the same length (placementArr.Length). Since you have 100 position values in your placementArr. That means you try to instantiate:

100 * 10 * 10 * 100 == 1,000,000 objects

You most likely want something like:

int i = 0;
for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f)
{
    for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f)
    {
        placementArr *= new Vector3 (xMin, 0, zMin);*

Instantiate(floorTile, placementArr*, Quaternion.identity);*
i++;
}
}
I don’t quite understand what that “placementArr” array is actually good for… You could just do:
for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f)
{
for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f)
{
Vector3 pos = new Vector3 (xMin, 0, zMin);
Instantiate(floorTile, pos, Quaternion.identity);
}
}

Let me try to break up your code and see what is happening.

The First loop, runs 100 times.

for (int i = 0; i < placementArr.Length; i++){...

The next two loops find the position of the tile.

for (float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f){
 
 
            for (float zMin = -450.0f; zMin < zMax; zMin = zMin + 100.0f){
 
                     placementArr *= new Vector3 (xMin, 0, zMin);*

The last loop runs 100 times and each time Instantiates floorTile at same position.
So there are 100 floorTiles at position xMin and zMin.
for (int iTwo = 0; iTwo < placementArr.Length; iTwo++) {
Instantiate(floorTile, placementArr[iTwo], Quaternion.identity);
Since first loop runs 100 times, there are 100100 objects at one location and 1001010100 total objects!
Lets try to create a floor.
Basic Idea : Create a floor tile at X and Z position.
Change X and Z.
Repeat.
We need two variable,
1) One would keep track of row
2) Second would keep track of column.
So, create a floor tile for entire row.
Go to next row.
Create tile for that row.
Repeat.
So, lets take some variables.

  • private float xMax = 450.0f;*
  • private float zMax = 450.0f;*

xMin would work as row.
zMin would work as column.

Now, we need to loop xMin (row) from starting to xMax. This will take care of the rows.

  •  for (float xMin = -450.0f ; xMin < xMax; xMin = xMin + 100.0f)* 
    
  •  {*
    
  •  	...*
    
  •  }*
    

And to take care of columns keep incrementing zMin inside xMin loop.

  •  	for( float zMin = -450.0f; zMin < zMax ; zMin = zMin + 100.0f)*
    
  •  	{*
    

  •  	}*
    

Instantiate the object inside the zMin loop at position (xMin,0,zMin).
Instantiate(floorTile, new Vector3(xMin,0,zMin), Quaternion.identity);
That’s all you need.
Full Code :
public Transform floorTile;

  • private float xMax = 450.0f;*

  • private float zMax = 450.0f;*

  • void Start() {*

  •  for ( float xMin = -450.0f; xMin < xMax; xMin = xMin + 100.0f)* 
    
  •  {*
    
  •  	for( float zMin = -450.0f ; zMin < zMax ; zMin = zMin + 100.0f)*
    
  •  	{*
    
  •  		Instantiate(floorTile, new Vector3(xMin,0,zMin), Quaternion.identity);*
    
  •  	}*
    
  •  }*
    
  • }*