Perlin noise causing Unity to Freeze; am I doing something wrong?
So I'm trying to make a function that places tiles psuedorandomly in a tile map using perlin noise; problem is that whenever I call the function 'Mathf.PerlinNoise()' unity freezes and eventually crashes (Note: I've tried this with small values for 'size'). I've also had this problem with simpler code in the past and in different versions of unity; am I calling it wrong perhaps or might it be my computer?
public Vector2Int size;
void changeMap(int x, int y, Tile place)
{
tileMap.SetTile(new Vector3Int(x, y, 0), place);
}
void makeLevel(int X, int Y)
{
if (Input.GetButtonDown("Jump"))
{
for (x = 0; x < X; y++)
{
for (y = 0; y < Y; y++)
{
if(Mathf.PerlinNoise(x/X, y/Y) > 0.8)
{
changeMap(x, y, tile);
}
}
}
}
}
private void Update()
{
makeLevel(size.x,size.y);
}
Update: running PerlinNoise() once is fine.
Answer by Namey5 · Apr 25, 2020 at 06:55 AM
...
for (x = 0; x < X; y++)
{
for (y = 0; y < Y; y++)
{
...
The crash isn't to do with perlin noise, more the fact that you have an infinite loop (you run the outer loop until x reaches X, but never actually increment it - instead you increment y twice).
Your answer
Follow this Question
Related Questions
How do you generate a circle terrain mesh 0 Answers
Symmetrical perlin noise 1 Answer
Texture does not scale correctly on a mesh 0 Answers
Following the trail of Perlin Noise random movement? 1 Answer
Seamless Perlin Noise problem. SOLVED 0 Answers