привет в чем здесь может быть ошибка using UnityEngine;
using System.Collections.Generic;

public class TerrainGenerator1 : MonoBehaviour
{
    public int size = 256;
    public float scale = 20f;
    public float heightScale = 10f;
    public int octaves = 6;
    public float persistence = 0.5f;
    public float lacunarity = 2f;
    public int seed = 0;

    public Material terrainMaterial;
    public int maxLOD = 5;
    public float[] lodDistances = { 50f, 100f, 200f, 400f, 800f };

    private Dictionary<Vector2, TerrainChunk> terrainChunks = new Dictionary<Vector2, TerrainChunk>();
    private List<TerrainChunk> visibleChunks = new List<TerrainChunk>();

    private void Start()
    {
        Random.InitState(seed);
        UpdateVisibleChunks();
    }

    private void Update()
    {
        UpdateVisibleChunks();
    }

    void UpdateVisibleChunks()
    {
        Vector3 viewerPosition = Camera.main.transform.position;
        Vector2 viewerChunkCoord = new Vector2(Mathf.RoundToInt(viewerPosition.x / size), Mathf.RoundToInt(viewerPosition.z / size));

        for (int i = visibleChunks.Count - 1; i >= 0; i--)
        {
            if (!IsChunkVisible(visibleChunks[i].coord, viewerChunkCoord))
            {
                visibleChunks[i].SetVisible(false);
                visibleChunks.RemoveAt(i);
            }
        }

        for (int yOffset = -1; yOffset <= 1; yOffset++)
        {
            for (int xOffset = -1; xOffset <= 1; xOffset++)
            {
                Vector2 viewedChunkCoord = new Vector2(viewerChunkCoord.x + xOffset, viewerChunkCoord.y + yOffset);

                if (terrainChunks.ContainsKey(viewedChunkCoord))
                {
                    terrainChunks[viewedChunkCoord].UpdateLOD(viewerPosition);
                    if (!visibleChunks.Contains(terrainChunks[viewedChunkCoord]))
                    {
                        visibleChunks.Add(terrainChunks[viewedChunkCoord]);
                    }
                }
                else
                {
                    terrainChunks.Add(viewedChunkCoord, new TerrainChunk(viewedChunkCoord, size, transform, terrainMaterial, maxLOD, lodDistances));
                }
            }
        }
    }

    bool IsChunkVisible(Vector2 chunkCoord, Vector2 viewerChunkCoord)
    {
        return (chunkCoord - viewerChunkCoord).sqrMagnitude <= 2;
    }

    public float GetHeight(float x, float z)
    {
        float amplitude = 1;
        float frequency = 1;
        float noiseHeight = 0;

        for (int i = 0; i < octaves; i++)
        {
            float sampleX = (x - size / 2f) / scale * frequency;
            float sampleZ = (z - size / 2f) / scale * frequency;

            float perlinValue = Mathf.PerlinNoise(sampleX + seed, sampleZ + seed) * 2 - 1;
            noiseHeight += perlinValue * amplitude;

            amplitude *= persistence;
            frequency *= lacunarity;
        }

        return noiseHeight * heightScale;
    }
}

public class TerrainChunk
{
    public Vector2 coord;
    GameObject meshObject;
    Vector2 position;
    Bounds bounds;
    MeshFilter meshFilter;
    MeshRenderer meshRenderer;
    LODInfo[] detailLevels;
    LODMesh[] lodMeshes;
    int colliderLODIndex;
    bool hasSetCollider;

    public TerrainChunk(Vector2 coord, int size, Transform parent, Material material, int maxLOD, float[] lodDistances)
    {
        this.coord = coord;
        position = coord * size;
        bounds = new Bounds(position, Vector2.one * size);
        Vector3 positionV3 = new Vector3(position.x, 0, position.y);

        meshObject = new GameObject("Terrain Chunk");
        meshFilter = meshObject.AddComponent<MeshFilter>();
        meshRenderer = meshObject.AddComponent<MeshRenderer>();
        meshRenderer.material = material;
        meshObject.transform.position = positionV3;
        meshObject.transform.parent = parent;

        SetVisible(false);

        detailLevels = new LODInfo[maxLOD];
        for (int i = 0; i < detailLevels.Length; i++)
        {
            detailLevels[i] = new LODInfo(lodDistances[i], size, i);
        }

        lodMeshes = new LODMesh[detailLevels.Length];
        for (int i = 0; i < detailLevels.Length; i++)
        {
            lodMeshes[i] = new LODMesh(detailLevels[i].lod);
            if (i == colliderLODIndex)
            {
                lodMeshes[i].updateCallback += UpdateCollisionMesh;
            }
        }

        UpdateTerrainChunk();
    }

    public void UpdateTerrainChunk()
    {
        float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(Camera.main.transform.position));
        bool wasVisible = IsVisible();
        bool visible = viewerDstFromNearestEdge <= detailLevels[detailLevels.Length - 1].visibleDstThreshold;

        if (visible)
        {
            int lodIndex = 0;
            for (int i = 0; i < detailLevels.Length - 1; i++)
            {
                if (viewerDstFromNearestEdge > detailLevels[i].visibleDstThreshold)
                {
                    lodIndex = i + 1;
                }
                else
                {
                    break;
                }
            }

            if (lodIndex != lodMeshes[lodIndex].lod)
            {
                LODMesh lodMesh = lodMeshes[lodIndex];
                if (lodMesh.hasMesh)
                {
                    meshFilter.mesh = lodMesh.mesh;
                }
                else if (!lodMesh.hasRequestedMesh)
                {
                    lodMesh.RequestMesh();
                }
            }
        }

        if (wasVisible != visible)
        {
            SetVisible(visible);
        }
    }

    public void UpdateLOD(Vector3 viewerPosition)
    {
        float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition));
        int lodIndex = 0;

        for (int i = 0; i < detailLevels.Length - 1; i++)
        {
            if (viewerDstFromNearestEdge > detailLevels[i].visibleDstThreshold)
            {
                lodIndex = i + 1;
            }
            else
            {
                break;
            }
        }

        if (lodIndex != lodMeshes[lodIndex].lod)
        {
            LODMesh lodMesh = lodMeshes[lodIndex];
            if (lodMesh.hasMesh)
            {
                meshFilter.mesh = lodMesh.mesh;
            }
            else if (!lodMesh.hasRequestedMesh)
            {
                lodMesh.RequestMesh();
            }
        }
    }

    public void SetVisible(bool visible)
    {
        meshObject.SetActive(visible);
    }

    public bool IsVisible()
    {
        return meshObject.activeSelf;
    }

    void UpdateCollisionMesh()
    {
        if (!hasSetCollider)
        {
            hasSetCollider = true;
            meshObject.AddComponent<MeshCollider>();
        }
    }

    class LODMesh
    {
        public Mesh mesh;
        public bool hasRequestedMesh;
        public bool hasMesh;
        public int lod;
        public event System.Action updateCallback;

        public LODMesh(int lod)
        {
            this.lod = lod;
        }

        void OnMeshDataReceived(MeshData meshData)
        {
            mesh = meshData.CreateMesh();
            hasMesh = true;

            updateCallback?.Invoke();
        }

        public void RequestMesh()
        {
            hasRequestedMesh = true;
            TerrainGenerator1 terrainGenerator = FindObjectOfType<TerrainGenerator1>();
            // Here you would generate the mesh data based on the LOD level
            // For simplicity, we're using the same mesh data for all LOD levels
            MeshData meshData = GenerateMeshData(terrainGenerator, lod);
            OnMeshDataReceived(meshData);
        }

        MeshData GenerateMeshData(TerrainGenerator1 terrainGenerator, int lod)
        {
            int meshSimplificationIncrement = (lod == 0) ? 1 : lod * 2;
            int verticesPerLine = (terrainGenerator.size - 1) / meshSimplificationIncrement + 1;

            MeshData meshData = new MeshData(verticesPerLine);
            int vertexIndex = 0;

            for (int y = 0; y < terrainGenerator.size; y += meshSimplificationIncrement)
            {
                for (int x = 0; x < terrainGenerator.size; x += meshSimplificationIncrement)
                {
                    float height = terrainGenerator.GetHeight(x, y);
                    meshData.vertices[vertexIndex] = new Vector3(x, height, y);
                    meshData.uvs[vertexIndex] = new Vector2(x / (float)terrainGenerator.size, y / (float)terrainGenerator.size);

                    if (x < terrainGenerator.size - 1 && y < terrainGenerator.size - 1)
                    {
                        meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine);
                        meshData.AddTriangle(vertexIndex + verticesPerLine + 1, vertexIndex, vertexIndex + 1);
                    }

                    vertexIndex++;
                }
            }

            return meshData;
        }
    }

    struct LODInfo
    {
        public float visibleDstThreshold;
        public int lod;

        public LODInfo(float visibleDstThreshold, int meshSize, int lod)
        {
            this.visibleDstThreshold = visibleDstThreshold;
            this.lod = lod;
        }
    }
}

public class MeshData
{
    public Vector3[] vertices;
    public int[] triangles;
    public Vector2[] uvs;

    int triangleIndex;

    public MeshData(int meshWidth)
    {
        vertices = new Vector3[meshWidth * meshWidth];
        uvs = new Vector2[meshWidth * meshWidth];
        triangles = new int[(meshWidth - 1) * (meshWidth - 1) * 6];
    }

    public void AddTriangle(int a, int b, int c)
    {
        triangles[triangleIndex] = a;
        triangles[triangleIndex + 1] = b;
        triangles[triangleIndex + 2] = c;
        triangleIndex += 3;
    }

    public Mesh CreateMesh()
    {
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;
        mesh.RecalculateNormals();
        return mesh;
    }
}