Error CS0161: Not all code paths return a value?

Edit: Thread Answered

Hi, so I am try to make a voxel-based game and this section of code is ment for face checking.

bool CheckVoxel (Vector3 pos) {
        int x = Mathf.FloorToInt (pos.x);
        int y = Mathf.FloorToInt (pos.y);
        int z = Mathf.FloorToInt (pos.z);

        if (x < 0 || x > VoxelData.ChunkWidth - 1 || y < 0 || y > VoxelData.ChunkHeight - 1 || z < 0 || z > VoxelData.ChunkWidth - 1)
              return false;
return voxelMap [x, y, z];
         }
}

So, I was wondering if anyone could help me. I was following a YouTube tutorial to make this. I asked the content creator for help, I am just waiting for a response. I figured Ixd ask here as well.

Here is the error and warning Unity threw my way:

WARNING:
[01:14:29] …\Chunk.cs(51,3): Unreachable Code Detected

ERROR:
[01:14:29] …\Chunk.cs(43,7): error CS0161: ‘Chunk.CheckVoxel(Vector3)’: not all code paths return a value

Here is a link to the code provided by the YouTube Content Creator:

I hope this is enough information.

return voxelMap [x, y, z]; in right after return false; Move that to outside of the conditional.

Not sure if this is the problem, hard to read the formatting.

Replace the current ‘Chunk’ script with the class below. (I’ve marked the edit at line 62).

using System.Collections.Generic;
using UnityEngine;

public class Chunk : MonoBehaviour {

    public MeshRenderer meshRenderer;
    public MeshFilter meshFilter;

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

    bool[,,] voxelMap = new bool[VoxelData.ChunkWidth, VoxelData.ChunkHeight, VoxelData.ChunkWidth];

    void Start() {

        PopulateVoxelMap();
        CreateMeshData();
        CreateMesh();

    }

    void PopulateVoxelMap() {

        for (int y = 0; y < VoxelData.ChunkHeight; y++) {
            for (int x = 0; x < VoxelData.ChunkWidth; x++) {
                for (int z = 0; z < VoxelData.ChunkWidth; z++) {

                    voxelMap[x, y, z] = true;

                }
            }
        }

    }

    void CreateMeshData() {

        for (int y = 0; y < VoxelData.ChunkHeight; y++) {
            for (int x = 0; x < VoxelData.ChunkWidth; x++) {
                for (int z = 0; z < VoxelData.ChunkWidth; z++) {

                    AddVoxelDataToChunk(new Vector3(x, y, z));

                }
            }
        }

    }

    bool CheckVoxel(Vector3 pos) {

        int x = Mathf.FloorToInt(pos.x);
        int y = Mathf.FloorToInt(pos.y);
        int z = Mathf.FloorToInt(pos.z);

        if (x < 0 || x > VoxelData.ChunkWidth - 1 || y < 0 || y > VoxelData.ChunkHeight - 1 || z < 0 || z > VoxelData.ChunkWidth - 1) {
            return false;
        }
        // ****
        return voxelMap[x, y, z];

    }

    void AddVoxelDataToChunk(Vector3 pos) {

        for (int p = 0; p < 6; p++) {

            if (!CheckVoxel(pos + VoxelData.faceChecks[p])) {

                vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 0]]);
                vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 1]]);
                vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 2]]);
                vertices.Add(pos + VoxelData.voxelVerts[VoxelData.voxelTris[p, 3]]);
                uvs.Add(VoxelData.voxelUvs[0]);
                uvs.Add(VoxelData.voxelUvs[1]);
                uvs.Add(VoxelData.voxelUvs[2]);
                uvs.Add(VoxelData.voxelUvs[3]);
                triangles.Add(vertexIndex);
                triangles.Add(vertexIndex + 1);
                triangles.Add(vertexIndex + 2);
                triangles.Add(vertexIndex + 2);
                triangles.Add(vertexIndex + 1);
                triangles.Add(vertexIndex + 3);
                vertexIndex += 4;

            }
        }

    }

    void CreateMesh() {

        Mesh mesh = new Mesh();
        mesh.vertices = vertices.ToArray();
        mesh.triangles = triangles.ToArray();
        mesh.uv = uvs.ToArray();

        mesh.RecalculateNormals();

        meshFilter.mesh = mesh;

    }

}
2 Likes

Thanks so much VectorBox, it worked. Looks like I had my blinders on lol. Thanks you to Chris-Trueman.

1 Like

Hey

Hey, so it got rid of the error but now for some reason my textures aren’t displaying correctly. Can you help? This is what it is doing:

EDIT: I fixed it, I had to fix something in my other script thanks m8!

1 Like