Instantiating in a for loop is causing an infinite loop?
ok, so I have been trying to figure this out for two days now. The for loop in my StartingFloor() function works fine if I comment out the instantiate like, but otherwise, it crashes.
var Tile : GameObject; var gameStart = true; function StartingFloor(FloorTile : GameObject) { var tilesInRow = 7; var rowsInColumn = 7; var HALF_ROW = 3; var TILE_SIZE = 0.24; var tileY = 0; for(var tileX = 0; tileX < tilesInRow; tileX++) { var tilePosition = Vector3(-1 (HALF_ROW TILE_SIZE) + (tileX * TILE_SIZE), tileY, 0); Instantiate(FloorTile, tilePosition, FloorTile.transform.rotation); if((tileX - (tilesInRow - 1)) % tilesInRow == 0) { tileY += 1; } yield WaitForSeconds(3); Debug.Log(tileX); } } function Start () { StartingFloor(Tile); }
Posting code using correct tools provided will make it more readable and more likely to get answered.
Answer by Ventext · Sep 28, 2016 at 12:59 AM
I AM SUCH AN IDIOT!!!! I attached the script for instantiating the tiles to the tiles. So every time one spawned, it spawned another just like it, and so on and so forth, causing infinite objects to spawn. I easily fixed the problem by creating an empty object named "TileSpawner" and attached the script to that, getting rid of the infinite instantiating problem. Gosh I can be so dumb sometimes.
Answer by lawrence-parry · Sep 19, 2016 at 01:54 AM
For a start when posting a question use the 101 button to add code like this:
var Tile : GameObject;
var gameStart = true;
function StartingFloor(FloorTile : GameObject)
{
var tilesInRow = 7;
var rowsInColumn = 7;
var HALF_ROW = 3;
var TILE_SIZE = 0.24;
var tileY = 0;
for(var tileX = 0; tileX < tilesInRow; tileX++)
{
var tilePosition = Vector3(-1 (HALF_ROW TILE_SIZE) + (tileX * TILE_SIZE), tileY, 0);
Instantiate(FloorTile, tilePosition, FloorTile.transform.rotation);
if((tileX - (tilesInRow - 1)) % tilesInRow == 0) {
tileY += 1;
}
yield WaitForSeconds(3);
Debug.Log(tileX);
}
}
function Start ()
{
StartingFloor(Tile);
}
As for what the problem is I'm not too sure, make sure the tile position and rotation you are setting is valid, try testing it with the position and rotation set to 000 or something. So for testing purposes try replacing your instantiate with:
Instantiate(FloorTile, Vector3.zero, Quaternion.identity);
If that fixes the crash then that lets you know the problem is the position and/or rotation you are setting. Hope this helps :)
I tried that. I even tried simply instantiating the tile with no other perimeters whatsoever, just Instantiate(FloorTile)
but it still causes an infinite loop. Thanks for the help though. (and telling me about the 101 button. can you tell i'm new to all of this?)
Your answer
Follow this Question
Related Questions
How to break reference from .asset classes and runtime copied values? 2 Answers
How to get objects to spawn in according to rules 1 Answer
Trying to name a gameObject - loop suddenly exiting. 1 Answer
Store instantiated object in List 2 Answers
Instantiating prefab causes existing instances to change values 1 Answer