For loop freezing the editor.

Hi,

I’m in the learning phase of my Unity experience so I’m pretty sure I’m making a basic mistake. If someone out there can help, it would be most appreciated.

When I run this code things are fine (although too many objects are created):

function Start() {

	CreateStranded("p_spaceMan", 10);
}

function CreateStranded(strandedObject, separationDistance){
	var selectedGameObject = GameObject.Find(strandedObject);

		Instantiate (selectedGameObject, Vector3(Random.Range(-separationDistance, separationDistance),0,Random.Range(-separationDistance, separationDistance)), Quaternion.identity);
	}
}

So I want to limit the number of objects being created by using a for loop:

public final var MAX_SPACEMEN = 20;

function Start() {

	CreateStranded("p_spaceMan", 10);

}

function CreateStranded(strandedObject, separationDistance){
	var selectedGameObject = GameObject.Find(strandedObject);

	for(var i=0; i<MAX_SPACEMEN; i++){
		Instantiate (selectedGameObject, Vector3(Random.Range(-separationDistance, separationDistance),0,Random.Range(-separationDistance, separationDistance)), Quaternion.identity);
	}
}

Once the for loop is in there Unity freezes. If I change the Start function to Update it runs but the objects are forever being created.

Is anyone able to point out where I’m going wrong?

Cheers,
Dan.

If that script is attached to the game object named p_spaceMan, which i assume it is, then 10 clones will be made for every clone made. So basically, it will go like this: 1 x 10 x 10 x 10 x 10 x 10 x . . .

Thanks Dreamblur, I knew it was going to be a simple mistake :slight_smile: