プレイヤーがいる位置のタイルを得たいのですが、うまくいきません。
タイルを得るために、タイルマップにTilemapSc.cs、プレイキャラにtile_player.csを紐づけたのですが、できません。
このサイトのコードと比較しても違いがないように思うのですが、なぜうまくいかないのでしょうか。グリッドとプレイキャラはヒエラルキーでは同階層で、グリッドの下階層にタイルマップがあります。
ヒエラルキー
|
├ Grid
│└Tile map ~ TilemapSc.cs
└player ~ tile_player.cs
TilemapSc.cs
unity2D
1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.Tilemaps; 6 7public class TilemapSc : MonoBehaviour { 8 public static Tilemap map; 9 10 void Start(){ 11 map = GetComponent<Tilemap> (); 12 if (map == null) 13 Debug.Log ("map is null"); 14 } 15}
tile_player.cs
unity2D
1 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using UnityEngine.Tilemaps; 6 7public class tile_player : MonoBehaviour { 8 Tilemap map; 9 // Use this for initialitation 10 void Start () { 11 map = TilemapSc.map; 12 //Debug.Log (gameObject.map.ToString ()); 13 } 14 15 // Update is called once per frame 16 void Update () { 17 if (Input.GetMouseButton (0)) { 18 Vector3 p_pos = this.transform.position; 19 Vector3Int pos = new Vector3Int (); 20 pos.x = (int)p_pos.x; 21 pos.y = (int)p_pos.y; 22 pos.z = (int)p_pos.z; 23 string msg = pos.ToString (); 24 Debug.Log (msg); 25 Tile t = (Tile)map.GetTile(pos); 26 if (t == null) 27 msg = "get tile incorrect"; 28 else 29 msg = t.sprite.ToString (); 30 Debug.Log (msg); 31 } 32 } 33} 34
発生したエラーのメッセージ
NullReferenceException: Object reference not set to an instance of an object
tile_player.Update () (at Assets/Script/example/tile_player.cs:24)
回答1件
あなたの回答
tips
プレビュー