Having trouble with a texture not showing on a generated mesh.

I am having a lot of trouble with perlin noise right now.
This is the code I am using to generate the noise:

using System.Collections;
using System.Collections.Generic;
using UnityEditor.PackageManager.UI;
using UnityEngine;

public class NoiseTextureGenerator : MonoBehaviour
{
    Texture2D noiseTexture;
    public Texture2D GenerateNoise(int pixelWidth, int pixelHeight, float xOrigin, float yOrigin, float scale)
    {
        Color[] colors = new Color[pixelWidth * pixelHeight];
        noiseTexture = new Texture2D(pixelWidth, pixelHeight);
        for (float x = 0.0f; x < noiseTexture.width; x++ )
        {
            for(float y = 0.0f; y < noiseTexture.height; y++)
            {
                float xCoord = xOrigin + x / noiseTexture.width * scale;
                float yCoord = yOrigin + y / noiseTexture.height * scale;
                float sample = Mathf.PerlinNoise(xCoord, yCoord);
                colors[Mathf.RoundToInt(y) * noiseTexture.width + Mathf.RoundToInt(x)] = new Color(sample, sample, sample);
            }
        }
        noiseTexture.SetPixels(colors);
        noiseTexture.Apply();
        return noiseTexture;
    }
}

This is the script I am using the generate the mesh:

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

public class MeshGenerator : MonoBehaviour
{
    Mesh mesh;
    Vector3[] vertices;
    int[] triangles;

    public Mesh GenerateMesh(int mapWidth, int mapHeight)
    {
        mesh = new Mesh();
        vertices = new Vector3[(mapHeight + 1) * (mapWidth + 1)];
        triangles = new int[6 * (mapHeight * mapWidth)];
        int index = 0;
        for (int x = 0; x <= mapHeight; x++)
        {
            for (int y = 0; y <= mapWidth; y++)
            {
                vertices[index] = new Vector3(x, 0, y);
                index += 1;
            }
        }
        int z = 0;
        int a = 0;
        for (int i = 0; i < triangles.Length; i += 6)
        {
            if (z == mapWidth)
            {
                triangles *= a + 1;*

triangles[i + 1] = a + 2;
triangles[i + 2] = a + mapWidth + 3;
triangles[i + 3] = a + 1;
triangles[i + 4] = a + mapWidth + 3;
triangles[i + 5] = a + mapWidth + 2;
z = 1;
a += 2;
}
else
{
triangles = a;
triangles[i + 1] = a + 1;
triangles[i + 2] = a + mapWidth + 2;
triangles[i + 3] = a;
triangles[i + 4] = a + mapWidth + 2;
triangles[i + 5] = a + mapWidth + 1;
z++;
a++;
}
}
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
return mesh;
}
}

Both of these scripts are called by this one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CreateMesh : MonoBehaviour
{
Texture2D texture2D;
public int mapWidth;
public int mapHeight;
public int textureResX;
public int textureResY;
public Vector2 textureOrigins;
public float textureScale;
public Material material;
void Start()
{
GameObject terrainMesh = new GameObject();
terrainMesh.transform.position = new Vector3(0, 0, 0);
terrainMesh.AddComponent();
terrainMesh.GetComponent().sharedMaterial = material;
terrainMesh.AddComponent();
MeshFilter meshFilter = terrainMesh.GetComponent();
MeshGenerator meshGenerator = FindObjectOfType();
Mesh mesh = meshGenerator.GenerateMesh(mapWidth, mapHeight);
meshFilter.mesh = mesh;
NoiseTextureGenerator noiseTextureGenerator = FindObjectOfType();
texture2D = noiseTextureGenerator.GenerateNoise(textureResX, textureResY, textureOrigins.x, textureOrigins.y, textureScale);
terrainMesh.GetComponent().sharedMaterial.mainTexture = texture2D;
}
}

For my material variable on the previous script, I am using a simple unlit texture. When I generate the mesh, it just appears grey, but if I open the material instance on the mesh renderer, the texture that it shows looks like perlin noise. So I guess my question is: Why isn’t the texture being applied to the mesh when it is applied to the material instance?
If you look at the picture below, you’ll see what I mean about the texture on the mesh not matching the texture on the material.
[182017-unity-screenshot.png|182017]_
_

Turns out I just needed to generate the uvs for my mesh.

Since this isn’t a very helpful conclusion to this problem, I had this exact same issue as described and was able to solve the issue using the following.

// Fill in UVs for Mesh
Vector2[] uvs = new Vector2[someMesh.vertices.Length];
for(int i = 0; i < uvs.Length; i++) {
    uvs[i] = new Vector2(someMesh.vertices[i].x / maxX, someMesh.vertices[i].z / maxZ);
}
someMesh.uv = uvs;

Procedurally generated textures began applying properly to procedurally generated meshes after this fix. maxX is the highest x-value found among the vectors within someMesh.vertices and maxZ is the same but for the z-value. This assumes the minimum x and z values that can be found in someMesh.vertices are 0. You may need to do some math depending on circumstance, but the goal is to map the UV values between 0.0 and 1.0. Do more reading on UVs and how they work for more info, but that’s the jist of it.