実現したいこと
他スクリプトの構造体をエラーなく取得したい
発生している問題・分からないこと
ゲームのステージを構造体で定義し、それを取得しようとしているときにエラーが出た、
インデックスを3などの定数にしても同じエラーが出ている。
エラーメッセージ
error
1NullReferenceException: Object reference not set to an instance of an object
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class GetStagecode : MonoBehaviour 6{ 7 [SerializeField] Stagecode stagecodesc; 8 [SerializeField] Numbermanager numbermanager; 9 int N=0; 10 // Start is called before the first frame update 11 void Start() 12 { 13 Debug.Log("クラスが呼び出された"); 14 if (numbermanager != null) 15 { 16 17 N = numbermanager.StageNumber - 1; 18 Debug.Log(N); 19 if (stagecodesc != null) 20 { 21 Stagecode.Stages stage = stagecodesc.Code[N];//エラー発生個所 22 Debug.Log(stage); 23 } 24 else 25 { 26 Debug.Log("stagecode is null"); 27 } 28 } 29 else 30 { 31 Debug.Log("numbermanager is null"); 32 } 33 34 35 36 37 } 38 39 // Update is called once per frame 40 void Update() 41 { 42 43 } 44}
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Stagecode : MonoBehaviour { GameObject obj; private Stages[] Stagecodes; void Start() { GameObject Block = (GameObject)Resources.Load("Block"); GameObject Short = (GameObject)Resources.Load("Short"); GameObject Long = (GameObject)Resources.Load("Long"); GameObject Hole = (GameObject)Resources.Load("Hole"); GameObject HardBlock = (GameObject)Resources.Load("HardBlock"); Stagecodes = new Stages[]{ new Stages(new Gimmicks[] { new Gimmicks(3, 3, Block, 0,0) }, new Pieces[] { new Pieces(Short, 0, 1),new Pieces(Long, 0, 1) }, null , "はじめのいっぽ"), new Pieces[]{ new Pieces(Short,135,1),new Pieces(Long,90,1),new Pieces(Short,315,1),new Pieces(Long,270,1)}, null , "風車"), new Stages(new Gimmicks[] { new Gimmicks(4, 1, Block, 0,0), new Gimmicks(1, 2, Block, 0,0), new Gimmicks(2, 2,Block, 0,0), new Gimmicks(5, 2, Hole, 0,0), new Gimmicks(2, 3, Short, 270,0), new Gimmicks(3, 3, Block, 0,0), new Gimmicks(4, 3, Short, 90,0),new Gimmicks(2, 4, Short, 315,0),new Gimmicks(5, 4, Block, 0,0),new Gimmicks(2, 5,Block, 0,0)}, null, new Remixes[] { new Remixes( new RemixElement[] { new RemixElement(Long, 90), new RemixElement(Short, 180) } ,1), new Remixes(new RemixElement[] { new RemixElement(Short, 45), new RemixElement(Short, 270) }, 1), new Remixes(new RemixElement[] { new RemixElement(Short, 0), new RemixElement(Short, 135) }, 2) } , "オーバーキル"), }; } public Stages[] Code { get { return Stagecodes; } private set { Stagecodes = value; } } // Update is called once per frame public struct Gimmicks { public int x; public int y; public GameObject Gimmick; public int direction; public int Extra; public Gimmicks(int x,int y, GameObject Gimmick,int direction,int Extra) { this.x = x; this.y = y; this.Gimmick = Gimmick; this.direction = direction; this.Extra = Extra; } } public struct Pieces { public GameObject Piece; public int direction; public int amount; public Pieces(GameObject Piece,int direction, int amount ) { this.amount = amount; this.direction = direction; this.Piece = Piece; } } public struct RemixElement { public GameObject Piece; public int direction; public RemixElement(GameObject Piece,int direction ) { this.direction = direction; this.Piece = Piece ; } } public struct Remixes { public RemixElement[] remixelement; public int amount; public Remixes(RemixElement[] remixelements,int amount) { this.remixelement = remixelements; this.amount = amount; } } public struct Stages { public Gimmicks[] gimmicks; public Pieces[] pieces; public Remixes[] remixes; public string Stagename; public Stages(Gimmicks[] gimmicks, Pieces[] pieces, Remixes[] remixes, string Stagename) { this.gimmicks = gimmicks; this.pieces = pieces; this.remixes = remixes; this.Stagename = Stagename; } } }
ここでは省略してますが、Stagecodesの要素数は10個くらいあります
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
nullチェックを追加したが、それを貫通し該当のコードが実行されてしまう.
インスペクター上ではスクリプトがアタッチされていることをしっかりと確認した。
補足
特になし
回答1件
あなたの回答
tips
プレビュー