Voxel Rendering Script Isn't Working Anymore

Source code from this video:

https://www.youtube.com/watch?v=b_1ZlHrJZc4|start=474

I understand that it’s 2 years old, but this script just doesn’t work. Help please!

The Code:

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

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class VoxelRender : MonoBehaviour
{
    Mesh mesh;
    List<Vector3>[] vertices;
    List<int>[] triangles;

    public float scale = 1f;

    float adjScale;


    void Awake()
    {
        mesh = GetComponent<MeshFilter>().mesh;
        adjScale = scale * 0.5f;

    }
    void Start()
    {
        GenerateVoxelMesh(new VoxelData());
        UpdateMesh();
    }

    void GenerateVoxelMesh(VoxelData data)
    {
        vertices = new List<Vector3>();
        triangles = new List<int>();

        for (int z = 0; z < data.Depth; z++)
        {
            for (int x = 0; x < data.Width; x++)
            {
                if (data.GetCell(x, z) == 0)
                {
                    continue;
                }
                MakeCube(adjScale, new Vector3((float)x * scale, 0, (float)z * scale));
            }
        }
    }

    void MakeCube(float cubeScale, Vector3 cubePos)
    {

        for (int i = 0; i < 6; i++)
        {
            MakeFace(i, cubeScale, cubePos);
        }
    }

    void MakeFace(int dir, float faceScale, Vector3 facePos)
    {
        vertices.AddRange(CubeMeshData.faceVertices(dir, faceScale, facePos));
        int vCount = vertices.Count;

        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 1);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4 + 3);
    }

    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices.toArray;
        mesh.triangles = triangles.toArray;
        mesh.RecalculateNormals();
    }
}
  1. When you post code use code tags
  2. “doesn’t work” tells us nothing. Does it just do nothing? Does it cause a compile error? An exception? Does it do something but not the right thing?

It says that I’m trying to call a method or access a class member that doesn’t exist for this part in particular -

        vertices.AddRange(CubeMeshData.faceVertices(dir, faceScale, facePos));
        int vCount = vertices.Count;

        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 1);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4 + 3);
    }

    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices.toArray;
        mesh.triangles = triangles.toArray;
        mesh.RecalculateNormals();
    }
}

It says this for “add”, “count”, “addRange”, and “toArray”

I wasn’t aware of those tags. Fixed reply -
It says that I’m trying to call a method or access a class member that doesn’t exist for this part in particular -

        vertices.AddRange(CubeMeshData.faceVertices(dir, faceScale, facePos));
        int vCount = vertices.Count;

        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 1);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4);
        triangles.Add(vCount - 4 + 2);
        triangles.Add(vCount - 4 + 3);
    }

    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices.toArray;
        mesh.triangles = triangles.toArray;
        mesh.RecalculateNormals();
    }
}

It says this for “add”, “count”, “addRange”, and “toArray”
Also, for this part too.

        vertices = new List<Vector3>();
        triangles = new List<int>();

It says it cannot implicitly convert System.Collections.Generic.List to System.Collections.Generic.List[ ]. I’m relatively new to this, so forgive me if this happens to be a stupid question.

Your vertices and triangles are arrays because of the [ ] you inserted into the line that shouldn’t be there. Arrays don’t have Add, etc, but List<> does.

(Would tell you what line numbers they are, but no code tags on the first post…)

Well, that’s petty. Would you like it if I fixed it? Would you then tell me what line numbers they are?

Lines 9 and 10

You say “petty”, I say “trying to make it easier for me to help you”

Sorry if the reply came off as upset- I am very bad with my voice online.

Okay. After that, two errors remain which can be chalked up to one error. Here it is-

    void UpdateMesh()
    {
        mesh.Clear();

        mesh.vertices = vertices.toArray;
        mesh.triangles = triangles.toArray;
        mesh.RecalculateNormals();
    }

It says that both List and List do not contain a definition for toArray. It was a capitalization error. The issue has become that it cannot convert method group ToArray to a non-delegate type Vector3[ ] - same for int[ ] but swap vector3 for int.

Capital T, and they need () after them

1 Like

Thank you so much!