Script not being accessed by other scripts

I’m so sorry to bother you guys, but i’m incredibly stuck. so i’m trying to make triangle indexes for my terrain mesh, but i’m stuck here because one script isn’t being accessed by others, here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class MeshGenerator
{
    public static MeshData GenerateTerrainMesh(float[,] heightMap)
    {
        int width = heightMap.GetLength(0);
        int height = heightMap.GetLength(1);
        float topLeftX = (width - 1) / -2f;
        float topLeftZ = (height - 1) / 2f;

        MeshData meshData = new MeshData(width, height);
        int vertexIndex = 0;

        for (int y = 0; y < width; y++)
        {
            for (int x = 0;x <height; x++)
            {
                meshData.vertices[vertexIndex] = new Vector3(topLeftX + x, heightMap[x, y], topLeftZ - y);
                meshData.uvs[vertexIndex] = new Vector2(x / (float)width, y / (float)height);

                if (x < width - 1 && y < height - 1)
                {
                    meshData.AddTriangle(vertexIndex, vertexIndex + width + 1, vertexIndex + width);
                    meshData.AddTriangle(vertexIndex + width + 1, vertexIndex, vertexIndex + 1);
                }

                vertexIndex++;
            }
        }

        return meshData;
    }

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

        int triangleIndex;

        public MeshData(int meshWidth, int meshHeight)
        {
            vertices = new Vector3[meshWidth * meshHeight];
            uvs = new Vector2[meshWidth * meshHeight];
            triangles = new int[(meshWidth - 1) * (meshHeight - 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 CreatMesh()
        {
            Mesh mesh = new Mesh ();
            mesh.vertices = vertices;
            mesh.triangles = triangles;
            mesh.uv = uvs;
            mesh.RecalculateNormals();
            return mesh;
        }
    }

}

that was the mesh thing, this is the thing that is trying to access it:

using UnityEngine;
using System.Collections;

public class TerrainDisplay : MonoBehaviour
{

    public Renderer textureRenderer;
    public MeshFilter meshFilter;
    public MeshRenderer meshRenderer;

    public void DrawTexture(Texture2D texture)
    {
        textureRenderer.sharedMaterial.mainTexture = texture;
        textureRenderer.transform.localScale = new Vector3(texture.width, 1, texture.height);
    }

    public void DrawMesh(MeshData meshData, Texture2D texture)
    {
        meshFilter.sharedMesh = meshData.CreatMesh();
        meshRenderer.sharedMaterial.mainTexture = texture;
    }
}

if you could help that would be greatly appreciated, but yeah I can’t figure it out

Hi @Val1dAnswer,

Your code sample is missing some things, so I’m going from my guts here, please correct me if I’m wrong.

If you call your DrawMesh(MeshData meshData, ..) from lets say the Update() function, how do you initialize the actual meshData object?

What you should do, in this order:

  • Create your MeshData object data, with calling MeshGenerator.CreateMesh();

  • Add all of it to your Mesh object, by calling data.CreateMesh();