- Home /
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[i] = 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 () {
}
}
The loops look ok, but personally I would avoid float loops because of rounding.
Regardless, put a debug inside the loop ins$$anonymous$$d of an instantiate call. If the loops run then you know the problem is the instantiation, otherwise you know its the loops.
Although I'm not sure if this is the cause. I'm pretty sure you're instantiating way more than just 100 object. The innermost loop would run 100 times, but then it's inside 3 other loops, with the outermost will also run 100 times. Unless I'm reading the thing wrong, pretty sure that's what happened.
Answer by Kartik1607 · Dec 19, 2014 at 06:19 AM
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[i] = 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 100*100 objects at one location and 100*10*10*100 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);
}
}
}
Thanks, this fixed it. Also thanks for walking me through it step by step so I understand what's going on. I have a tendency to over-complicate things.
Answer by Bunny83 · Dec 19, 2014 at 04:56 AM
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[i] = new Vector3 (xMin, 0, zMin);
Instantiate(floorTile, placementArr[i], 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);
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Autocomplete a for-loop in MonoDevelop? 1 Answer
forloop running more than said amout of times 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer