unity-game-engineperlin-noise

Unity loop Application.EnterPlayMode


When I hit Play Unity makes me wait infinite time before start the scene. The message in Hold On window is "Application.EnterPlayMode Waiting for Unity's code to finish executing". There is only one scene in URP with global volume, directional light and a plane with this script:

using UnityEngine;

public class PerlinNoise : MonoBehaviour
{
    [Header("Resolution")]
    [Space]
    public int width = 256;
    public int heigth = 256;
    [Space]
    [Header("Adjustments")]
    [Space]
    public float scale = 20;
    public float xOffset = 10;
    public float yOffset = 10;

 void Update()
 {
     Renderer renderer = GetComponent<Renderer>();
     renderer.material.mainTexture = GenerateTexture();
 }

 Texture2D GenerateTexture()
 {
     Texture2D texture = new Texture2D(width, heigth);

     for (int x = 0; x < width; x++)
     {
         for (int y = 0; x < heigth; y++)
         {
             Color color = GenerateColor(x, y);
             texture.SetPixel(width, heigth, color);
         }
     }

     texture.Apply();
     return texture;
 }

 Color GenerateColor(int x, int y)
 {
     float xCoord = (float)x / width * scale + xOffset;
     float yCoord = (float)y / width * scale + yOffset;
     float perlinNoise = Mathf.PerlinNoise(xCoord, yCoord);
     return new Color(perlinNoise, perlinNoise, perlinNoise);
 }
}

I tried to kill unity editor task in task manager and restart unity but the same issue repeats. Please help me


Solution

  • You have an infinite loop inside your GenerateTexture method. Specifically, the condition in the nested loop (for y) is accidentally checking for x:

    for (int x = 0; x < width; x++)
    {
        for (int y = 0; x < heigth; y++) // x < height SHOULD BE y < height
        {
            Color color = GenerateColor(x, y);
            texture.SetPixel(width, heigth, color); // this should probably be (x, y, color)
        }
    }