Why wont unity respond after i added this simple logic?

i added this script to move all the blocks i my editor to another location, but unity now gives me the beachball of death like it its stcuk in a loop. what happened?

function Update () {
	var septoggle = Input.GetAxis("separate");
	if (septoggle != 1) {
		for (var voxel : GameObject in voxelArray){ //iterates through objects in voxelArray
			var voxelLoc = voxel.GetComponent(voxelScript);//gets voxel origin location
			
			voxel.transform.position = Vector3(voxelLoc.x, voxelLoc.y * sep * septoggle, voxelLoc.z);	
		}
	}
}

How many items are in voxelArray?

–

There aren't any typeless variables in that code. It probably already has #pragma strict, or if not, it won't make any difference here.

–

turns out this code wasn't the problem, i removed #Pragma strict and the block above (generating voxel array) stopped running. i had to remove it because of the multiplication you see there. it thought one of those was an object preventing me to multiply it. ill try converting it to a value Voxelscript is the script kept on the voxels themselves to store variables and such. is there a better way to do this?

–

Do not remove #pragma strict. Write code that works properly with it.

–

1 Answer

1

The Update method is called 30 times per second (depending on your settings). So your move logic is running 1000’s of times, that’s why you see nothing else.

Perhaps you wanted to make the objects move only once. You can do that by moving your code to your Start() method. Then it will execute only once.

Thank you, it turns out the block of code i had before was reacting negatively to me removing #pragma strict. is there any way to have a funcion called only when, say, a key is pressed?

–