Search Unity

Question This is a very confusing error

Discussion in 'Scripting' started by Afterlife301209, Jan 14, 2022.

  1. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    45
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. //error =
    4. NullReferenceException: Object reference not set to an instance of an object
    5. MeshGenerator.GenerateTerrainMesh (System.Single[,] heightMap, System.Single heightMultiplier, UnityEngine.AnimationCurve heightCurve, System.Int32 levelOfDetail) (at Assets/Scripts/Procedural Landmass Generation/MeshGenerator.cs:20)
    6. MapGenerator.GenerateMap () (at Assets/Scripts/Procedural Landmass Generation/MapGenerator.cs:51)
    7. MapGeneratorEditor.OnInspectorGUI () (at Assets/Scripts/Procedural Landmass Generation/Editor/MapGeneratorEditor.cs:16)
    8. UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <1ada6c7052bb42378c5ec1bd01fc4723>:0)
    9. UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
    10.  
    11.  
    12. public class MapGenerator : MonoBehaviour {
    13.  
    14.     public enum DrawMode {NoiseMap, ColourMap, Mesh};
    15.     public DrawMode drawMode;
    16.  
    17.     public int mapWidth;
    18.     public int mapHeight;
    19.     public float noiseScale;
    20.  
    21.     public float meshHeightMultiplier;
    22.  
    23.     public int octaves;
    24.     [Range(0,1)]
    25.     public float persistance;
    26.     public float lacunarity;
    27.  
    28.     public int seed;
    29.     public Vector2 offset;
    30.  
    31.     public bool autoUpdate;
    32.  
    33.     public TerrainType[] regions;
    34.  
    35.     public void GenerateMap() {
    36.         float[,] noiseMap = Noise.GenerateNoiseMap (mapWidth, mapHeight, seed, noiseScale, octaves,  persistance, lacunarity, offset);
    37.  
    38.         Color[] colourMap = new Color[mapWidth * mapHeight];
    39.         for (int y = 0; y < mapHeight; y++) {
    40.             for (int x = 0; x < mapWidth; x++) {
    41.                 float currentHeight = noiseMap [x, y];
    42.                 for (int i = 0; i < regions.Length; i++) {
    43.                     if (currentHeight <= regions [i].height) {
    44.                         colourMap [y * mapWidth + x] = regions [i].colour;
    45.                         break;
    46.                     }
    47.                 }
    48.             }
    49.         }
    50.  
    51.         MapDisplay display = FindObjectOfType<MapDisplay> ();
    52.         if (drawMode == DrawMode.NoiseMap) {
    53.             display.DrawTexture (TextureGenerator.TextureFromHeightMap (noiseMap));
    54.         } else if (drawMode == DrawMode.ColourMap) {
    55.             display.DrawTexture (TextureGenerator.TextureFromColourMap (colourMap, mapWidth, mapHeight));
    56.         } else if (drawMode == DrawMode.Mesh) {
    57.             display.DrawMesh (MeshGenerator.GenerateTerrainMesh (noiseMap, meshHeightMultiplier), TextureGenerator.TextureFromColourMap (colourMap, mapWidth, mapHeight));
    58.         }
    59.     }
    60.  
    61.     void OnValidate() {
    62.         if (mapWidth < 1) {
    63.             mapWidth = 1;
    64.         }
    65.         if (mapHeight < 1) {
    66.             mapHeight = 1;
    67.         }
    68.         if (lacunarity < 1) {
    69.             lacunarity = 1;
    70.         }
    71.         if (octaves < 0) {
    72.             octaves = 0;
    73.         }
    74.     }
    75. }
    76.  
    77. [System.Serializable]
    78. public struct TerrainType {
    79.     public string name;
    80.     public float height;
    81.     public Color colour;
    82. }
     
  2. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    45
    The error is
    NullReferenceException: Object reference not set to an instance of an object
    MeshGenerator.GenerateTerrainMesh (System.Single[,] heightMap, System.Single heightMultiplier, UnityEngine.AnimationCurve heightCurve, System.Int32 levelOfDetail) (at Assets/Scripts/Procedural Landmass Generation/MeshGenerator.cs:20)
    MapGenerator.GenerateMap () (at Assets/Scripts/Procedural Landmass Generation/MapGenerator.cs:51)
    MapGeneratorEditor.OnInspectorGUI () (at Assets/Scripts/Procedural Landmass Generation/Editor/MapGeneratorEditor.cs:16)
    UnityEditor.UIElements.InspectorElement+<>c__DisplayClass59_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <1ada6c7052bb42378c5ec1bd01fc4723>:0)
    UnityEngine.GUIUtility:processEvent(Int32, IntPtr, Boolean&)
     
  3. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    @Afterlife301209 The error is in MeshGenerator.cs on line 20 and perhaps MapGeneratorEditor.cs on line 16 but you shared the code for MapGenerator.cs.
     
  4. Afterlife301209

    Afterlife301209

    Joined:
    Nov 19, 2021
    Posts:
    45
    Ohh sorry here is the code of mesh gen -
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public static class MeshGenerator {
    5.  
    6.  
    7.     public static MeshData GenerateTerrainMesh(float[,] heightMap)
    8.     {
    9.         int width = heightMap.GetLength (0);
    10.         int height = heightMap.GetLength (1);
    11.         float topLeftX = (width - 1) / -2f;
    12.         float topLeftZ = (height - 1) / 2f;
    13.  
    14.         MeshData meshData = new MeshData (width, height);
    15.         int vertexIndex = 0;
    16.  
    17.         for (int y = 0; y < height; y++)
    18.         {
    19.             for (int x = 0; x < width; x++)
    20.             {
    21.  
    22.                 meshData.vertices [vertexIndex] = new Vector3 (topLeftX + x, heightMap [x, y], topLeftZ - y);
    23.                 meshData.uvs [vertexIndex] = new Vector2 (x / (float)width, y / (float)height);
    24.  
    25.                 if (x < width - 1 && y < height - 1) {
    26.                     meshData.AddTriangle (vertexIndex, vertexIndex + width + 1, vertexIndex + width);
    27.                     meshData.AddTriangle (vertexIndex + width + 1, vertexIndex, vertexIndex + 1);
    28.                 }
    29.  
    30.                 vertexIndex++;
    31.             }
    32.         }
    33.  
    34.         return meshData;
    35.  
    36.     }
    37. }
    38.  
    39. public class MeshData
    40. {
    41.     public Vector3[] vertices;
    42.     public int[] triangles;
    43.     public Vector2[] uvs;
    44.  
    45.     int triangleIndex;
    46.  
    47.     public MeshData(int meshWidth, int meshHeight)
    48.     {
    49.         vertices = new Vector3[meshWidth * meshHeight];
    50.         uvs = new Vector2[meshWidth * meshHeight];
    51.         triangles = new int[(meshWidth-1)*(meshHeight-1)*6];
    52.     }
    53.  
    54.     public void AddTriangle(int a, int b, int c)
    55.     {
    56.         triangles [triangleIndex] = a;
    57.         triangles [triangleIndex + 1] = b;
    58.         triangles [triangleIndex + 2] = c;
    59.         triangleIndex += 3;
    60.     }
    61.  
    62.     public Mesh CreateMesh()
    63.     {
    64.         Mesh mesh = new Mesh ();
    65.         mesh.vertices = vertices;
    66.         mesh.triangles = triangles;
    67.         mesh.uv = uvs;
    68.         mesh.RecalculateNormals ();
    69.         return mesh;
    70.     }
    71.  
    72. }