- Home /
Generating 3x3 "Terrain" using PerlinNoise
My code:
var block:GameObject;
var blockMax=5;
function Start(){
for(var i=0;i<3;i++){
for(var e=0;e<3;e++){
Instantiate(block,Vector3(i,Mathf.Floor(blockMax*Mathf.PerlinNoise(1/i,1/e)),e),block.transform.rotation);
}
}
Note that I have the block GameObject assigned to a textured cube in the editor.
My intention is for this code to generate 3x3 set of blocks that are each 1 "unit" away from each other based on their number in the i and e of the loops. Their height should be determined by PerlinNoise based on their location on the X and Z axes. When I run the single Instantiate function by itself with values plugged in it works as intended, but whenever I put it in a loop the Unity editor (and eventually my computer) crashes and I am forced to restart. I'm assuming it's creating some kind of recursive/infinite loop (explaining the crash) but I don't see what I am doing wrong. Is there a point where I made a problem with the code and it fails to finish the loops, or is there another problem, what is it and how can I fix it?
Not sure but you have two divide by zero errors there from looking at your code.
PerlinNoise(1/i,1/e) both i and e will be zero at the beginning of your loops.
Your answer
![](https://koobas.hobune.stream/wayback/20220613151116im_/https://answers.unity.com/themes/thub/images/avi.jpg)