World generation

This is a script that is supposed to create the floor around the player if there is not a block there already but it freezes up unity

#pragma strict

var X : int;
var Y : int;
var Z : int;

var grassBlock : GameObject;
var hill : GameObject;

function Start()
{
	while(1)
	{
		for(var i=(X-64) ; i<(X+64) ; i++)
		{
			for(var j=(Z-64) ; j<(Z+64) ; j++)
			{
				if (!(Physics.Raycast (Vector3(i,0,j+2), Vector3.back, 2 )))
				{
					Instantiate (grassBlock, Vector3(i,0,j), Quaternion.identity);
				}
			}
		}
		yield;
	}
}

function Update () 
{
	X = transform.position.x;
	Z = transform.position.z;
}

while(1) is an infinite loop. Not escaping that loop is why unity is freezing

@brianruggieri - The while(1) loop won't freeze the app because of the 'yield'.

Ah. Didn't pick that out when the code was unformatted

1 Answer

1

You are doing 16k of Raycasts (128 * 128) each frame. That is a lot raycasts. But even worse, if all those raycast fail, you are creating 16K of game objects. That will freeze the app…at least for awhile. Not sure of the nature of your game, but you may want to look to an alternate solution for creating your terrain.