editor script makes editor crash

i’m trying to make an editor script that will take a heightmap and turn it in to a cubic terrain but when i run the script the editor crashes.

#pragma strict
class CubeLand extends EditorWindow{
	
	var texture:Texture2D;
	var index:int = 0;
	var options:String[] = ["full", "half", "quarter"];
	var obj:GameObject;
	var size:Vector3;
	
	@MenuItem ("Window/CubeLand")
	
	static function ShowWindow(){
		EditorWindow.GetWindow (CubeLand);
	}
	
	function OnGUI () {
		texture = EditorGUI.ObjectField(Rect(3, 3, Screen.width, 100), "HeightMap", texture, Texture, false);
		index = EditorGUI.Popup(Rect(3, 106, Screen.width, 50), "Resolution: ", index, options);
		if(index == 0){
			//if full res
			if(GUI.Button(Rect(0, Screen.height - Screen.height/10, Screen.width, Screen.height/10), "Generate")){
				for(var x:int = 0; x < texture.height; x++){
					for(var z:int = 0; z < texture.width; z++){
						obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
						obj.transform.position = Vector3(x, 0, z);
						obj.transform.position.y = Mathf.RoundToInt(texture.GetPixel(x, z).grayscale * 15);
					}
				}
			}
		}else if(index == 1){
			//if half res
			if(GUI.Button(Rect(0, Screen.height - Screen.height/10, Screen.width, Screen.height/10), "Generate")){
				for(var x2:int = 0; x2 < texture.height/2; x2++){
					for(var z2:int = 0; z2 < texture.width/2; z2++){
						obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
						obj.transform.position = Vector3(x2, 0, z2);
						obj.transform.position.y = Mathf.RoundToInt(texture.GetPixel(x, z).grayscale * 15);
					}
				}
			}
		}else if(index == 2){
			//if quarter res
			if(GUI.Button(Rect(0, Screen.height - Screen.height/10, Screen.width, Screen.height/10), "Generate")){
				for(var x3:int = 0; x3 < texture.height/4; x3++){
					for(var z3:int = 0; z3 < texture.width/4; z3++){
						obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
						obj.transform.position = Vector3(x3, 0, z3);
						obj.transform.position.y = Mathf.RoundToInt(texture.GetPixel(x, z).grayscale * 15);
					}
				}
			}
		}
	}
}

Well, “when i run the script” is not very specific. Does that mean when you open the editor window? Or when you click one of your buttons? I guess the latter. Also i don’t think that the editor crashes, but simply freezes during the double loop.

Keep in mind if you have a 1024x1024 texture you would create one million gameobjects. That’s way too much and could actually take several minutes if not longer. Also you would get a horrible performance. Terrains should always be build out of one or a few meshes and you simply changing the vertices / triangles / quads in that mesh.

Also there’s another thing wrong, which however shouldn’t cause a crash. You have your height / width reversed. The x component, when you pass it to GetPixel, is in the range of 0 to texture.width-1. You however let x go from 0 to texture.height. If the texture is square that doesn’t matter, but if it isn’t the function would throw an exception.

Another thing about performance is that you should use GetPixels and work with the returned array. using GetPixel in a loop is significantly slower than using GetPixels once and use the array to read the individual pixels.