質問をすることでしか得られない、回答やアドバイスがある。

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

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

Q&A

解決済

1回答

1360閲覧

Unityで変数に二次元配列の要素を代入しようとするとエラーが出る

roua

総合スコア22

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity

Unityは、Unity Technologiesが開発・販売している、IDEを内蔵するゲームエンジンです。主にC#を用いたプログラミングでコンテンツの開発が可能です。

配列

配列は、各データの要素(値または変数)が連続的に並べられたデータ構造です。各配列は添え字(INDEX)で識別されています。

0グッド

0クリップ

投稿2019/09/25 11:09

前提・実現したいこと

Unity2Dにて音ゲーを作っているんですが、変数に2次元配列の要素を代入しようとすると
IndexOutOfRangeException: Index was outside the bounds of the array.
Humen1.Jikkou1 () (at Assets/Humen1.cs:93)
Humen1+<Junbi>d__16.MoveNext () (at Assets/Humen1.cs:53)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)
というエラーが出てしまいます。
どなたか教えていただけると幸いです。

発生している問題・エラーメッセージ

IndexOutOfRangeException: Index was outside the bounds of the array. Humen1.Jikkou1 () (at Assets/Humen1.cs:93) Humen1+<Junbi>d__16.MoveNext () (at Assets/Humen1.cs:53) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

該当のソースコード

C#

1//93/53行目 2using System.Collections; 3using System.Collections.Generic; 4using UnityEngine; 5using System; 6 7public class Humen1 : MonoBehaviour 8{ 9 public GameObject Notes; 10 public GameObject Notes2; 11 public GameObject Notes3; 12 public GameObject Notes4; 13 public GameObject Notes5; 14 public GameObject Notes6; 15 public GameObject Notes7; 16 public GameObject Notes8; 17 public GameObject Notes9; 18 public GameObject Notes10; 19 public int z = 0; 20 int x = 0; 21 /* 22 float a; 23 float b; 24 float c; 25 float d; 26 float e; 27 float a1; 28 float b1; 29 float c1; 30 float d1; 31 float e1; 32 */ 33 // Start is called before the first frame update 34 35 void Start() 36 { 37 StartCoroutine(Wait(1f)); 38 StartCoroutine(Junbi()); 39 FadeManager.FadeIn(); 40 } 41 42 // Update is called once per frame 43 public int Jikkou 44 { 45 // 取得 46 get { return z; } 47 48 // 変更 49 set { z = value; } 50 } 51 public IEnumerator Junbi() 52 { 53 yield return new WaitUntil(Z); 54 Jikkou1(); 55 } 56 public void Jikkou1() 57 { 58 int a; 59 int b; 60 int c; 61 float d; 62 float e; 63 float[,] notes1; 64 notes1 = new float[22,5] { 65 {4,2,2,-7.4f,6}, 66 {4,3,2,3.7f,-6}, 67 {5,0,2,7.4f,6}, 68 {5,1,2,-3.7f,-6}, 69 {5,2,2,0,-6}, 70 {5,3,0,-3.7f,6}, 71 {5,3,0,3.7f,6}, 72 {5,3,2,0,-6}, 73 {6,0,2,3.7f,-6}, 74 {6,0,2,-3.7f,-6}, 75 {6,1,0,0,6}, 76 {6,1,2,3.7f,-6}, 77 {6,1,2,-3.7f,-6}, 78 {6,2,2,0,-6}, 79 {6,3,0,-7.4f,6}, 80 {6,3,0,7.4f,6}, 81 {6,3,2,0,-6}, 82 {7,0,2,7.4f,-6}, 83 {7,0,2,-7.4f,-6}, 84 {7,1,0,0,6}, 85 {7,1,2,7.4f,-6}, 86 {7,1,2,-7.4f,-6}, 87 88 89 //{, , , , } 90 }; 91 //int y = notes1.Length; 92 for (int i = 0; i < notes1.Length; ++i) 93 { 94 a = (int)notes1[i,x]; 95 ++x; 96 b = (int)notes1[i, x]; 97 ++x; 98 c = (int)notes1[i, x]; 99 ++x; 100 d = notes1[i, x]; 101 ++x; 102 e = notes1[i, x]; 103 x = 0; 104 105 if (Mathf.Approximately(e, 6.0f)) 106 { 107 switch (d) 108 { 109 case -7.4f: 110 GameObject notes = Instantiate(Notes, new Vector3(d, 6.0f, 0.0f), Quaternion.identity) as GameObject; 111 NotesIdou a_1 = notes.GetComponent<NotesIdou>(); 112 a_1.Idou(a, b, c); 113 break; 114 case -3.7f: 115 GameObject notes2 = Instantiate(Notes2, new Vector3(d, 6.0f, 0.0f), Quaternion.identity) as GameObject; 116 NotesIdou2 a_2 = notes2.GetComponent<NotesIdou2>(); 117 a_2.Idou(a, b, c); 118 break; 119 case 0: 120 GameObject notes3 = Instantiate(Notes3, new Vector3(d, 6.0f, 0.0f), Quaternion.identity) as GameObject; 121 NotesIdou3 a_3 = notes3.GetComponent<NotesIdou3>(); 122 a_3.Idou(a, b, c); 123 break; 124 case 3.7f: 125 GameObject notes4 = Instantiate(Notes4, new Vector3(d, 6.0f, 0.0f), Quaternion.identity) as GameObject; 126 NotesIdou4 a_4 = notes4.GetComponent<NotesIdou4>(); 127 a_4.Idou(a, b, c); 128 break; 129 case 7.4f: 130 GameObject notes5 = Instantiate(Notes5, new Vector3(d, 6.0f, 0.0f), Quaternion.identity) as GameObject; 131 NotesIdou5 a_5 = notes5.GetComponent<NotesIdou5>(); 132 a_5.Idou(a, b, c); 133 break; 134 } 135 }else{ 136 //if (Mathf.Approximately(e, -6.0f) 137 switch (d) 138 { 139 case -7.4f: 140 GameObject notes6 = Instantiate(Notes6, new Vector3(d, -6.0f, 0.0f), Quaternion.identity) as GameObject; 141 NotesIdou6 a_6 = notes6.GetComponent<NotesIdou6>(); 142 a_6.Idou(a, b, c); 143 break; 144 case -3.7f: 145 GameObject notes7 = Instantiate(Notes7, new Vector3(d, -6.0f, 0.0f), Quaternion.identity) as GameObject; 146 NotesIdou7 a_7 = notes7.GetComponent<NotesIdou7>(); 147 a_7.Idou(a, b, c); 148 break; 149 case 0: 150 GameObject notes8 = Instantiate(Notes8, new Vector3(d, -6.0f, 0.0f), Quaternion.identity) as GameObject; 151 NotesIdou8 a_8 = notes8.GetComponent<NotesIdou8>(); 152 a_8.Idou(a, b, c); 153 break; 154 case 3.7f: 155 GameObject notes9 = Instantiate(Notes9, new Vector3(d, -6.0f, 0.0f), Quaternion.identity) as GameObject; 156 NotesIdou9 a_9 = notes9.GetComponent<NotesIdou9>(); 157 a_9.Idou(a, b, c); 158 break; 159 case 7.4f: 160 GameObject notes10 = Instantiate(Notes10, new Vector3(d, -6.0f, 0.0f), Quaternion.identity) as GameObject; 161 NotesIdou10 a_10 = notes10.GetComponent<NotesIdou10>(); 162 a_10.Idou(a, b, c); 163 break; 164 } 165 } 166 167 168 } 169 } 170 private bool Z() => z == 1; 171 IEnumerator Wait(float wait) 172 { 173 yield return new WaitForSeconds(wait); 174 } 175 176} 177 178 179

補足情報(FW/ツールのバージョンなど)

Unity
2019.2.5f1

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2019/09/25 14:43

エラーの1行目に書いてあるんですが、これ以上何を教えればいいんでしょうか?
roua

2019/09/26 01:05

このエラーコードで調べて、出てきたものはだいたい要素数が指定した数より多いというものでした。 ですが、僕は要素数を指定してないので... 結局なぜこのエラーが出たのかわからないんです。 動きはするんですがね...
guest

回答1

0

ベストアンサー

2次元配列の場合、notes1.Length で要素数を取ろうとすると返ってくる数値は要素の個数になるかと思います。

試しに Debug.Log(notes1.Length); を入れて数値を確認してみてください。このソースだと110が返ってきます。(22行x5列 = 110)
つまりfor文で回している間にiが23になった段階で参照先を失ってインデックス範囲外エラーとなります。

配列の長さを知りたい場合 .GetLength(次元数) で取得してください。

cs

1Debug.Log(notes1.GetLength(0)); // 22 2Debug.Log(notes1.GetLength(1)); // 5

投稿2019/09/26 01:03

編集2019/09/26 01:04
hogefugapiyo

総合スコア3302

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

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

roua

2019/09/26 01:21

ありがとうございます! 次元数取得で解決しました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問