error CS0103: The name [name] does not exist in the current context

carl4020

New member
Joined
Sep 22, 2020
Messages
3
Programming Experience
5-10
Hello. Basically i suck at code at times and i am very shit at troubleshooting. I get these errors in my MeshGenerator.cs:

Assets\Scripts\OpenWorld\Procedural Landmass Generation\MeshGenerator.cs(52,32): error CS0103: The name 'heightCurve' does not exist in the current context
Assets\Scripts\OpenWorld\Procedural Landmass Generation\MeshGenerator.cs(52,53): error CS0103: The name 'heightMap' does not exist in the current context
Assets\Scripts\OpenWorld\Procedural Landmass Generation\MeshGenerator.cs(52,72): error CS0103: The name 'heightMultiplier' does not exist in the current context
I posted my code to hatebin: hatebin
And down here line 52 is:
float height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;

Mod Edit - Added code from topic bin post :
C#:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

public static class MeshGenerator
{
    public static MeshData GenerateTerrainMesh(float[,] borderedSizeMap, float borderedSizeMultiplier, AnimationCurve _borderedSizeCurve, int levelOfDetail)
    {
        AnimationCurve borderedSizeCurve = new AnimationCurve(_borderedSizeCurve.keys);

        int borderedSize = borderedSizeMap.GetLength(0);
        int meshSize = borderedSize - 2;

        float topLeftX = (meshSize - 1) / -2f;
        float topLeftZ = (meshSize - 1) / 2f;

        int meshSimplificationIncrement = (levelOfDetail == 0) ? 1 : levelOfDetail * 2;
        int verticesPerLine = (meshSize - 1) / meshSimplificationIncrement + 1;

        MeshData meshData = new MeshData(verticesPerLine);

        int[,] vertexIndicesMap = new int[borderedSize, borderedSize];
        int meshVertexIndex = 0;
        int borderVertexIndex = -1;

        for (int y = 0; y < borderedSize; y += meshSimplificationIncrement)
        {
            for (int x = 0; x < borderedSize; x += meshSimplificationIncrement)
            {
                bool isBorderVertex = y == 0 || y == borderedSize - 1 || x == 0 || x == borderedSize - 1;

                if (isBorderVertex)
                {
                    vertexIndicesMap[x, y] = borderVertexIndex;
                    borderVertexIndex--;
                }
                else
                {
                    vertexIndicesMap[x, y] = meshVertexIndex;
                    meshVertexIndex++;
                }
            }
        }

                for (int y = 0; y < borderedSize; y+= meshSimplificationIncrement)
        {
            for (int x = 0; x < borderedSize; x+= meshSimplificationIncrement)
            {
                int vertexIndex = vertexIndicesMap[x, y];
                Vector2 percent = new Vector2((x - meshSimplificationIncrement) / (float)meshSize, (y - meshSimplificationIncrement) / (float)meshSize);
                float height = heightCurve.Evaluate(heightMap[x, y]) * heightMultiplier;
                Vector3 vertexPosition = new Vector3(topLeftX + percent.x * meshSize, height, topLeftZ - percent.y * meshSize);

                meshData.AddVertex(vertexPosition, percent, vertexIndex);
                
                if (x < borderedSize - 1 && y < borderedSize - 1)
                {
                    int a = vertexIndicesMap[x, y];
                    int b = vertexIndicesMap[x + meshSimplificationIncrement, y];
                    int c = vertexIndicesMap[x, y + meshSimplificationIncrement];
                    int d = vertexIndicesMap[x + meshSimplificationIncrement, y + meshSimplificationIncrement];
                    meshData.AddTriangle(a, d, c);
                    meshData.AddTriangle(d, a, b);
                }

                vertexIndex++;
            }
        }

        return meshData;

    }
}

public class MeshData
{
    Vector3[] vertices;
    int[] triangles;
    Vector2[] uvs;

    Vector3[] borderVertices;
    int[] borderTriangles;

    int triangleIndex;
    int borderTriangleIndex;

    public MeshData(int verticesPerLine)
    {
        vertices = new Vector3[verticesPerLine * verticesPerLine];
        uvs = new Vector2[verticesPerLine * verticesPerLine];
        triangles = new int[(verticesPerLine - 1) * (verticesPerLine - 1) * 6];

        borderVertices = new Vector3[verticesPerLine * 4 + 4];
        borderTriangles = new int[24 * verticesPerLine];
    }

    public void AddVertex(Vector3 vertexPosition, Vector2 uv, int vertexIndex)
    {
        if (vertexIndex < 0)
        {
            borderVertices[-vertexIndex - 1] = vertexPosition;
        }
        else
        {
            vertices[vertexIndex] = vertexPosition;
            uvs[vertexIndex] = uv;
        }
    }

    public void AddTriangle(int a, int b, int c)
    {
        if (a < 0 || b < 0 || c < 0)
        {
            borderTriangles[borderTriangleIndex] = a;
            borderTriangles[borderTriangleIndex + 1] = b;
            borderTriangles[borderTriangleIndex + 2] = c;
            borderTriangleIndex += 3;
        }
        else
        {
            triangles[triangleIndex] = a;
            triangles[triangleIndex + 1] = b;
            triangles[triangleIndex + 2] = c;
            triangleIndex += 3;
        }
    }

    Vector3[] CalculateNormals()
    {

        Vector3[] vertexNormals = new Vector3[vertices.Length];
        int triangleCount = triangles.Length / 3;
        for (int i = 0; i < triangleCount; i++)
        {
            int normalTriangleIndex = i * 3;
            int vertexIndexA = triangles[normalTriangleIndex];
            int vertexIndexB = triangles[normalTriangleIndex + 1];
            int vertexIndexC = triangles[normalTriangleIndex + 2];

            Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC);
            vertexNormals[vertexIndexA] += triangleNormal;
            vertexNormals[vertexIndexB] += triangleNormal;
            vertexNormals[vertexIndexC] += triangleNormal;
        }

        int borderTriangleCount = borderTriangles.Length / 3;
        for (int i = 0; i < triangleCount; i++)
        {
            int normalTriangleIndex = i * 3;
            int vertexIndexA = borderTriangles[normalTriangleIndex];
            int vertexIndexB = borderTriangles[normalTriangleIndex + 1];
            int vertexIndexC = borderTriangles[normalTriangleIndex + 2];

            Vector3 triangleNormal = SurfaceNormalFromIndices(vertexIndexA, vertexIndexB, vertexIndexC);
            if (vertexIndexA >= 0)
            {
                vertexNormals[vertexIndexA] += triangleNormal;
            }
            if (vertexIndexB >= 0)
            {
                vertexNormals[vertexIndexB] += triangleNormal;
            }
            if (vertexIndexC >= 0)
            {
                vertexNormals[vertexIndexC] += triangleNormal;
            }
        }

        for (int i = 0; i < vertexNormals.Length; i++)
        {
            vertexNormals[i].Normalize();
        }

        return vertexNormals;

    }

    Vector3 SurfaceNormalFromIndices(int indexA, int indexB, int indexC)
    {
        Vector3 pointA = (indexA < 0) ? borderVertices[-indexA - 1] : vertices[indexA];
        Vector3 pointB = (indexB < 0) ? borderVertices[-indexB - 1] : vertices[indexB];
        Vector3 pointC = (indexC < 0) ? borderVertices[-indexC - 1] : vertices[indexC];

        Vector3 sideAB = pointB - pointA;
        Vector3 sideAC = pointC - pointA;
        return Vector3.Cross(sideAB, sideAC).normalized;
    }

    public Mesh CreateMesh()
    {
        Mesh mesh = new Mesh();
        mesh.vertices = vertices;
        mesh.triangles = triangles;
        mesh.uv = uvs;
        mesh.normals = CalculateNormals();
        return mesh;
    }

}
 
Last edited by a moderator:
Please post your code in code tags here so that it'll have the same lifetime as this thread. Chances are that this thread will outlive your hatebin post.
 
It looks like you stole the code from here and started changing variable names:

 
Nope. I have never seen that page in my life. Please just help me instead of accusing me of stealing. I don't even understand that language.
 
If you're being told that those names don't exist in that context, the most likely reason is that those names don't exist in that context. Maybe show us the code in context, i.e. where exactly do you think those names are introduced such they should exist?

As a general piece of advice, maybe you should make some attempt to get better at coding and troubleshooting. Declaring the variables that you use in your code is about as fundamental a concept as there is in programming so, if you don't understand that then you don't understand programming. In that case, a mesh generator might be something you should leave alone for now and build up your knowledge and skills with more basic projects. If you try to run before you can walk, you're going to keep having to get others to tend to your skinned knees and that's not the way to learn. Speaking as someone who has and does spend a lot of time on the web helping people with programming problems, we'll tend to get tired of explaining stuff that you should already know but have ignored because you want to jump into the sexy stuff.
 
Last edited:
Nope. I have never seen that page in my life. Please just help me instead of accusing me of stealing. I don't even understand that language.
Okay, it may not have come from exactly that site, but it came from someplace else.

In your code, you are using:
C#:
public static MeshData GenerateTerrainMesh(float[,] borderedSizeMap, float borderedSizeMultiplier, AnimationCurve _borderedSizeCurve, int levelOfDetail)
{
    AnimationCurve borderedSizeCurve = new AnimationCurve(_borderedSizeCurve.keys);
and most other tutorials regarding mesh generation have this signature:
C#:
public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve _heightCurve, int levelOfDetail)
{
    AnimationCurve heightCurve = new AnimationCurve(_heightCurve.keys);

Hmmm. How odd that the the parameters line up. How odd that your line of code that doesn't compile has the variables heightMap and heightMultiplier and heightCurve.
 
Welcome to the website. I have brought the code from the bin post to our forums so it's not lost.

As an aside, you might find if you are a bit more forthcoming about where you got your code, you might find the guys a bit more helpful.

And if you admit that you got the code from Jack and Jill's website, we might be able to compare for irregularities.

We know you certainly didn't write this yourself, because if you did, you would know every inch of every line wrote in every block, and what its intended purpose was and how it is meant to be used.
 
I followed a tutorial. This one to be exact.
I didn't mean to claim this as my own code. So i am sorry if it looked like it.
 
The code for Episode 12 still uses heightMap, heightMultiplier, and _heightCurve
C#:
public static MeshData GenerateTerrainMesh(float[,] heightMap, float heightMultiplier, AnimationCurve _heightCurve, int levelOfDetail) {
    AnimationCurve heightCurve = new AnimationCurve (_heightCurve.keys);
 

Latest posts

Back
Top Bottom