質問するログイン新規登録

Q&A

解決済

1回答

325閲覧

NullReferenceException: Object reference not set to an instance of an objectが解決できない

Hotnoa

総合スコア3

0グッド

1クリップ

投稿2025/02/03 07:51

編集2025/02/03 10:05

0

1

実現したいこと

他スクリプトの構造体をエラーなく取得したい

発生している問題・分からないこと

ゲームのステージを構造体で定義し、それを取得しようとしているときにエラーが出た、
インデックスを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チェックを追加したが、それを貫通し該当のコードが実行されてしまう.
インスペクター上ではスクリプトがアタッチされていることをしっかりと確認した。

補足

特になし

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YAmaGNZ

2025/02/03 08:00

stagecodescに格納されるStagecodeというクラスはどこで作成されどこで初期化されるのでしょうか? その際Codeという配列(?)はどこで初期化されどこで値がセットされるのでしょうか?
fiveHundred

2025/02/03 08:13

Stagecodeのコードを記載してください。
Hotnoa

2025/02/03 10:06

コメントありがとうございます。Stagecodeのコードを追記しました、文字数の関係でstagecodesの要素数を一部省略いたしました、確認お願いします
guest

回答1

0

ベストアンサー

StagecodeのStart()でStagecodes(=Code)を初期化しているようですが、GetStagecodeのStart()が先に実行されると、その初期化の前にCodeが参照されるためエラーになります。
Start()よりも先に実行されるAwake()を使うか、Stagecodeが先に実行されるように設定を変更してください。

投稿2025/02/03 10:38

fiveHundred

総合スコア10480

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Hotnoa

2025/02/08 10:05

回答ありがとうございます。 ご指摘の通りAwakeの中にコードをコピペしたら問題が解決しました、ありがとうございました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問