Issue with a script which causes the editor to freeze on play

I have created a script (below) which when I press play causes the editor to go not responding, so far I have found that the error is in the spawn or delaydecrease function

#pragma strict
var objectToSpawn : GameObject;
var player : GameObject;
var playerZ : float;
var spawning : boolean = true;
var delay : float = 1;

function Start () {
//Spawn();
//DelayDecrease();
}

function Spawn () {
while (spawning)
	WaitForSeconds(delay);
	var object : GameObject = Instantiate(objectToSpawn, transform.position + Vector3(Random.Range(-85,85),0,0), transform.rotation);
}

function Update () {
player = GameObject.Find("Player");
playerZ = player.transform.position.z;
transform.position.z = playerZ + 150;
}

function DelayDecrease () {
while (spawning)
	WaitForSeconds(1);
	delay /= 1.1;
	delay = Mathf.Clamp(delay, 0.1, 1);
}

Please format your code using the code button (101010), otherwise it's very difficult for us to try to read it.

just FWIW. i think you have completely forgotten a set of braces at lines 27-28-29

1 Answer

1

You aren’t yielding your WaitForSeconds.

    yield WaitForSeconds(1);