Search code examples
c#unity-game-engine3dminecraftcube

Trying to make Minecraft(following tutorial) code not working


Here is my Chunk.cs code:

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

public class Chunk : MonoBehaviour
{
    public MeshRenderer meshRenderer;
    public MeshFilter meshFilter;

    void Start()
    {
        int vertexIndex = 0;
        List<Vector3> vertices = new List<Vector3>();
        List<int> triangles = new List<int>();
        List<Vector2> uvs = new List<Vector2>();

        for(int i = 0; i < 6; i++)
        {
            int triangleIndex = VoxelData.voxelTris[0, 1];
            vertices.Add(VoxelData.voxelVerts [triangleIndex]);
            triangles.Add(vertexIndex);

            uvs.Add(Vector2.zero);

            vertexIndex++;
        }
        Mesh mesh = new Mesh();
        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.uv = uvs.ToArray();
        mesh.RecalculateNormals ();

        meshFilter.mesh = mesh;
    }

}

Here is my VoxelData.cs code:

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

public static class VoxelData
{
    public static readonly Vector3[] voxelVerts = new Vector3[8]
    {
        new Vector3(0.0f, 0.0f, 0.0f),
        new Vector3(1.0f, 0.0f, 0.0f),
        new Vector3(1.0f, 1.0f, 0.0f),
        new Vector3(0.0f, 1.0f, 0.0f),
        new Vector3(0.0f, 0.0f, 1.0f),
        new Vector3(1.0f, 0.0f, 1.0f),
        new Vector3(1.0f, 1.0f, 1.0f),
        new Vector3(0.0f, 1.0f, 1.0f),

    };

    public static readonly int[,] voxelTris = new int[1, 6]
    {
        {3, 7, 2, 2, 7, 6}// Top face
    };
}

Here is the tutorial I have been following:

https://www.youtube.com/watch?v=h66IN1Pndd0&list=PLVsTSlfj0qsWEJ-5eMtXsYp03Y9yF1dEn&index=1

The error might be something related to the function on line 21, due to it is not yellow like the other add function:

vertices.Add(VoxelData.voxelVerts [triangleIndex]);

Thanks in advance


Solution

  • Since you didn't specify the error, I have to assume it is this

    int triangleIndex = VoxelData.voxelTris[0, 1];
    

    Which should be

    int triangleIndex = VoxelData.voxelTris[0, i];
    

    if that doesn't fix it then i think you should be more specific about the error.