前提・実現したいこと
エラーを解消したい
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object Map.CreateTiles () (at Assets/Script/Map.cs:45) Map.Start () (at Assets/Script/Map.cs:21)
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Map : MonoBehaviour 6{ 7 public int width; 8 public int height; 9 10 public Tile[,] tiles; 11 12 public Material material; 13 14 Mesh mesh; 15 16 private MeshPosition meshPosition = new MeshPosition(); 17 18 // Use this for initialization 19 void Start() 20 { 21 CreateTiles(); 22 GenerateMesh(); 23 foreach(Vector2 a in meshPosition.MeshPos) 24 { 25 Debug.Log(a); 26 } 27 } 28 29 // Update is called once per frame 30 void Update() 31 { 32 Graphics.DrawMesh(mesh, Vector3.zero, Quaternion.identity, material, 0); 33 } 34 35 void CreateTiles() 36 { 37 tiles = new Tile[width, height]; 38 39 for (int i = 0; i < width; i++) 40 { 41 for (int j = 0; j < height; j++) 42 { 43 tiles[i, j] = new Tile(Tile.Type.Dirt); 44 Vector2 pos = new Vector2(width, height); 45 meshPosition.MeshPos.Add(pos); 46 } 47 } 48 } 49 50 void GenerateMesh() 51 { 52 MeshData data = new MeshData(tiles); 53 mesh = new Mesh(); 54 mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; 55 mesh.vertices = data.vertices.ToArray(); 56 mesh.triangles = data.triangles.ToArray(); 57 mesh.uv = data.UVs.ToArray(); 58 mesh.RecalculateNormals(); 59 mesh.RecalculateBounds(); 60 } 61} 62
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class MeshPosition 6{ 7 private List<Vector2> meshPos; 8 9 public List<Vector2> MeshPos 10 { 11 get 12 { 13 return meshPos; 14 } 15 } 16} 17
試したこと
Map.csでデバッグしようとしているのですが、エラーになってしまいます。MeshPositionのListを使おうとしているだけなのですが…。どうしてNullReferenceExceptionになるのか、教えてもらえるとありがたいです。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/06/12 13:40