Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Resolved Voxel Rendering Script Isn't Working Anymore

Discussion in 'Scripting' started by Arrivaderci, May 14, 2020.

  1. Arrivaderci

    Arrivaderci

    Joined:
    May 14, 2020
    Posts:
    22
    Source code from this video:


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

    The Code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. [RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
    6. public class VoxelRender : MonoBehaviour
    7. {
    8.     Mesh mesh;
    9.     List<Vector3>[] vertices;
    10.     List<int>[] triangles;
    11.  
    12.     public float scale = 1f;
    13.  
    14.     float adjScale;
    15.  
    16.  
    17.     void Awake()
    18.     {
    19.         mesh = GetComponent<MeshFilter>().mesh;
    20.         adjScale = scale * 0.5f;
    21.  
    22.     }
    23.     void Start()
    24.     {
    25.         GenerateVoxelMesh(new VoxelData());
    26.         UpdateMesh();
    27.     }
    28.  
    29.     void GenerateVoxelMesh(VoxelData data)
    30.     {
    31.         vertices = new List<Vector3>();
    32.         triangles = new List<int>();
    33.  
    34.         for (int z = 0; z < data.Depth; z++)
    35.         {
    36.             for (int x = 0; x < data.Width; x++)
    37.             {
    38.                 if (data.GetCell(x, z) == 0)
    39.                 {
    40.                     continue;
    41.                 }
    42.                 MakeCube(adjScale, new Vector3((float)x * scale, 0, (float)z * scale));
    43.             }
    44.         }
    45.     }
    46.  
    47.     void MakeCube(float cubeScale, Vector3 cubePos)
    48.     {
    49.  
    50.         for (int i = 0; i < 6; i++)
    51.         {
    52.             MakeFace(i, cubeScale, cubePos);
    53.         }
    54.     }
    55.  
    56.     void MakeFace(int dir, float faceScale, Vector3 facePos)
    57.     {
    58.         vertices.AddRange(CubeMeshData.faceVertices(dir, faceScale, facePos));
    59.         int vCount = vertices.Count;
    60.  
    61.         triangles.Add(vCount - 4);
    62.         triangles.Add(vCount - 4 + 1);
    63.         triangles.Add(vCount - 4 + 2);
    64.         triangles.Add(vCount - 4);
    65.         triangles.Add(vCount - 4 + 2);
    66.         triangles.Add(vCount - 4 + 3);
    67.     }
    68.  
    69.     void UpdateMesh()
    70.     {
    71.         mesh.Clear();
    72.  
    73.         mesh.vertices = vertices.toArray;
    74.         mesh.triangles = triangles.toArray;
    75.         mesh.RecalculateNormals();
    76.     }
    77. }
     
    Last edited: May 14, 2020
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,670
    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?
     
  3. Arrivaderci

    Arrivaderci

    Joined:
    May 14, 2020
    Posts:
    22
    It says that I'm trying to call a method or access a class member that doesn't exist for this part in particular -
    Code (CSharp):
    1.         vertices.AddRange(CubeMeshData.faceVertices(dir, faceScale, facePos));
    2.         int vCount = vertices.Count;
    3.  
    4.         triangles.Add(vCount - 4);
    5.         triangles.Add(vCount - 4 + 1);
    6.         triangles.Add(vCount - 4 + 2);
    7.         triangles.Add(vCount - 4);
    8.         triangles.Add(vCount - 4 + 2);
    9.         triangles.Add(vCount - 4 + 3);
    10.     }
    11.  
    12.     void UpdateMesh()
    13.     {
    14.         mesh.Clear();
    15.  
    16.         mesh.vertices = vertices.toArray;
    17.         mesh.triangles = triangles.toArray;
    18.         mesh.RecalculateNormals();
    19.     }
    20. }
    It says this for "add", "count", "addRange", and "toArray"
     
    Last edited: May 14, 2020
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,670
     
  5. Arrivaderci

    Arrivaderci

    Joined:
    May 14, 2020
    Posts:
    22
    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 -
    Code (CSharp):
    1.         vertices.AddRange(CubeMeshData.faceVertices(dir, faceScale, facePos));
    2.         int vCount = vertices.Count;
    3.  
    4.         triangles.Add(vCount - 4);
    5.         triangles.Add(vCount - 4 + 1);
    6.         triangles.Add(vCount - 4 + 2);
    7.         triangles.Add(vCount - 4);
    8.         triangles.Add(vCount - 4 + 2);
    9.         triangles.Add(vCount - 4 + 3);
    10.     }
    11.  
    12.     void UpdateMesh()
    13.     {
    14.         mesh.Clear();
    15.  
    16.         mesh.vertices = vertices.toArray;
    17.         mesh.triangles = triangles.toArray;
    18.         mesh.RecalculateNormals();
    19.     }
    20. }
    21.  
    It says this for "add", "count", "addRange", and "toArray"
    Also, for this part too.
    Code (CSharp):
    1.         vertices = new List<Vector3>();
    2.         triangles = new List<int>();
    It says it cannot implicitly convert System.Collections.Generic.List<int> to System.Collections.Generic.List<int>[]. I'm relatively new to this, so forgive me if this happens to be a stupid question.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,670
    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...)
     
  7. Arrivaderci

    Arrivaderci

    Joined:
    May 14, 2020
    Posts:
    22
    Well, that's petty. Would you like it if I fixed it? Would you then tell me what line numbers they are?
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,670
    Lines 9 and 10

    You say "petty", I say "trying to make it easier for me to help you"
     
  9. Arrivaderci

    Arrivaderci

    Joined:
    May 14, 2020
    Posts:
    22
    Sorry if the reply came off as upset- I am very bad with my voice online.
     
  10. Arrivaderci

    Arrivaderci

    Joined:
    May 14, 2020
    Posts:
    22
    Okay. After that, two errors remain which can be chalked up to one error. Here it is-
    Code (CSharp):
    1.     void UpdateMesh()
    2.     {
    3.         mesh.Clear();
    4.  
    5.         mesh.vertices = vertices.toArray;
    6.         mesh.triangles = triangles.toArray;
    7.         mesh.RecalculateNormals();
    8.     }
    It says that both List<Vector3> and List<int> 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.
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,670
    Capital T, and they need () after them
     
    Arrivaderci likes this.
  12. Arrivaderci

    Arrivaderci

    Joined:
    May 14, 2020
    Posts:
    22
    Thank you so much!
     
unityunity