Creating cube in script/ texture stretches on all sides but top and bottom?

I’m trying to create a cube from scratch and everything is going well except when I add a material or texture to it. The texture is correct on the top and bottom faces, but stretches on all other sides. The texture appears correctly on Unity’s default cube, but I create the cubes in a way that not all faces are drawn.

I believe the problem may be incorrect UV mapping but I’m finding it quite difficult to understand how that works. The script is included below, and I appreciate any and all help!

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



public class BlockScript : MonoBehaviour {


    //Code for rendering mesh
    Vector3[] vertices = new Vector3[8];
    List<int> trianglesOne = new List<int>();
    Vector2[] uvs = new Vector2[8];
    Mesh mesh;

    public Material wood;

    

    //Vector3 pos;
    //End of code for rendering mesh

    ChunkScript chunkScript;
    public GameObject CoordChecker;

    int xPos;
    int yPos;
    int zPos;

    void AddComponents() {
        gameObject.AddComponent<MeshFilter>();
        gameObject.AddComponent<MeshRenderer>();
        gameObject.AddComponent<BoxCollider>();
        BoxCollider box = gameObject.GetComponent<BoxCollider>();
        box.size = new Vector3(.99f, .99f, .99f);
        mesh = GetComponent<MeshFilter>().mesh;
        if (GlobalVariableHolder.wood) gameObject.GetComponent<MeshRenderer>().material = wood;

        gameObject.tag = "Block";
    }
    void Start() {


        AddComponents();
    }

    public void printName() {
        print(name);
    }

    void OnTriggerEnter(Collider other) {
        if (other.tag == "Chunk") {
            transform.parent = other.transform;

            xPos = (int)transform.localPosition.x;
            yPos = (int)transform.localPosition.y;
            zPos = (int)transform.localPosition.z;

            transform.name = (xPos + " " + yPos + " " + zPos);
            


            chunkScript = transform.parent.GetComponent<ChunkScript>();
            chunkScript.blockPos[xPos, yPos, zPos] = true;
            CreateFaces();
            chunkScript.ChunkUpdate();
        }
    }

    void OnDestroy() {
        chunkScript.blockPos[xPos, yPos, zPos] = false;
    }


    //Checks if each direction has a block and calls the respective function to draw the face
    public void CreateFaces() {
        mesh.Clear();
        Vertices();

        if (zPos < 9) {
            if (chunkScript.blockPos[xPos, yPos, zPos + 1] == false) FaceNorth();
        } else if(zPos == 9) {
            GameObject checker = Instantiate(CoordChecker, new Vector3(xPos, yPos, zPos + 1), Quaternion.identity) as GameObject;
            CoordCheckerScript checkerScript = checker.GetComponent<CoordCheckerScript>();
            if (checkerScript.containsBlock == false) { FaceNorth(); }
            Destroy(checker.gameObject);
        }

        if (xPos < 9) {
            if (!chunkScript.blockPos[xPos + 1, yPos, zPos]) FaceEast();
        } else if(xPos == 9) {
            GameObject checker = Instantiate(CoordChecker, new Vector3(xPos + 1, yPos, zPos), Quaternion.identity) as GameObject;
            CoordCheckerScript checkerScript = checker.GetComponent<CoordCheckerScript>();
            if (checkerScript.containsBlock == false) { FaceWest(); }
            Destroy(checker.gameObject);
        }

        if (zPos > 0) {
            if (!chunkScript.blockPos[xPos, yPos, zPos - 1]) FaceSouth();
        } else if(zPos == 0) {
            GameObject checker = Instantiate(CoordChecker, new Vector3(xPos, yPos, zPos - 1), Quaternion.identity) as GameObject;
            CoordCheckerScript checkerScript = checker.GetComponent<CoordCheckerScript>();
            if (checkerScript.containsBlock == false) { FaceSouth(); }
            Destroy(checker.gameObject);
        }

        if (xPos > 0) {
            if (!chunkScript.blockPos[xPos - 1, yPos, zPos]) FaceWest();
        } else if (xPos == 0) {
            GameObject checker = Instantiate(CoordChecker, new Vector3(xPos - 1, yPos, zPos), Quaternion.identity) as GameObject;
            CoordCheckerScript checkerScript = checker.GetComponent<CoordCheckerScript>();
            if (checkerScript.containsBlock == false) { FaceWest(); }
            Destroy(checker.gameObject);
        }

        if (yPos < 299) {
            if (!chunkScript.blockPos[xPos, yPos + 1, zPos]) FaceTop();
        }

        if (yPos > 0) {
            if (!chunkScript.blockPos[xPos, yPos - 1, zPos]) FaceBottom();
        }

        int[] triangles = trianglesOne.ToArray();
        mesh.triangles = triangles;

    }

    //Code to set mesh triangles
    void Vertices() {
        vertices[0] = new Vector3(-0.5f, -0.5f, -0.5f);
        vertices[1] = new Vector3(-0.5f, 0.5f, -0.5f);
        vertices[2] = new Vector3(0.5f, 0.5f, -0.5f);
        vertices[3] = new Vector3(0.5f, -0.5f, -0.5f);

        vertices[4] = new Vector3(-0.5f, -0.5f, 0.5f);
        vertices[5] = new Vector3(-0.5f, 0.5f, 0.5f);
        vertices[6] = new Vector3(0.5f, 0.5f, 0.5f);
        vertices[7] = new Vector3(0.5f, -0.5f, 0.5f);

        mesh.vertices = vertices;

        for(int i = 0; i < vertices.Length; i++) {
            uvs <em>= new Vector2(vertices_.x, vertices*.z);*_</em>

}
mesh.uv = uvs;
}

void FaceNorth() {
trianglesOne.Add(7);
trianglesOne.Add(6);
trianglesOne.Add(5);
trianglesOne.Add(7);
trianglesOne.Add(5);
trianglesOne.Add(4);

}
void FaceEast() {
trianglesOne.Add(3);
trianglesOne.Add(2);
trianglesOne.Add(6);
trianglesOne.Add(3);
trianglesOne.Add(6);
trianglesOne.Add(7);
}
void FaceSouth() {
trianglesOne.Add(0);
trianglesOne.Add(1);
trianglesOne.Add(2);
trianglesOne.Add(0);
trianglesOne.Add(2);
trianglesOne.Add(3);
}
void FaceWest() {
trianglesOne.Add(4);
trianglesOne.Add(5);
trianglesOne.Add(1);
trianglesOne.Add(4);
trianglesOne.Add(1);
trianglesOne.Add(0);
}
void FaceTop() {
trianglesOne.Add(1);
trianglesOne.Add(5);
trianglesOne.Add(6);
trianglesOne.Add(1);
trianglesOne.Add(6);
trianglesOne.Add(2);
}
void FaceBottom() {
trianglesOne.Add(4);
trianglesOne.Add(0);
trianglesOne.Add(3);
trianglesOne.Add(4);
trianglesOne.Add(3);
trianglesOne.Add(7);
}

Unfortunately, as far as I know, the only way to procedurally UV a complex mesh is to split it apart into smaller meshes so that they are connected on the UV plane. If you aren’t aware, the UV coordinate system is like the XY system except that it goes from 0-1 for a typical texture (beyond 1 it tiles every whole number). To map a cube you can do it a couple ways, depending on what you need: map each face onto the entire 0-1 square, taking up the whole texture and repeating it on each face, or unfolding the cube into a “T” shape within the UV grid.

Either way you’ll likely need to split it up as I said because when you add UVs to a procedural mesh you do it one for each vertex. However, when unfolding a mesh there will be points that are shared, the same vertex represented by two points in UV space. For the case of the cube you could solve it by creating 6 squares, a total of 24 verts, and create the UVs for each based on your texture. A more efficient method would be to only double up on the vertices that have more than one UV point. And finally, you could always use a custom shader and save on the vertex count.