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

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

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

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

Unity

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

Q&A

解決済

1回答

1042閲覧

Unity2Dでの関数代入エラーの解決方法

roua

総合スコア22

C#

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

Unity

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

0グッド

0クリップ

投稿2019/09/21 00:00

前提・実現したいこと

プログラム86行目(以降)で変数にリストのx1番目を代入するというプログラムを書いたつもりなのですが、エラーが出ます。が、ちゃんと動きはするんです。超初歩的かもしれませんし、プログラムも汚いですがなぜエラーが出ているのか、教えていただけると幸いです。

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

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <599589bf4ce248909b8a14cbe4a2034e>:0) System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <599589bf4ce248909b8a14cbe4a2034e>:0) Humen1+<Jikkou1_2>d__19.MoveNext () (at Assets/Humen1.cs:86) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at /Users/builduser/buildslave/unity/build/Runtime/Export/Scripting/Coroutines.cs:17)

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System.Linq; 5using UnityEngine.UI; 6using System; 7 8public class Humen1 : MonoBehaviour 9{ 10 public GameObject Notes; 11 public GameObject Notes2; 12 public GameObject Notes3; 13 public GameObject Notes4; 14 public GameObject Notes5; 15 public GameObject Notes6; 16 public GameObject Notes7; 17 public GameObject Notes8; 18 public GameObject Notes9; 19 public GameObject Notes10; 20 21 public int z = 0; 22 int x = 0; 23 int x1 = 0;/* 24 float a; 25 float b; 26 float c; 27 float d; 28 float e; 29 float a1; 30 float b1; 31 float c1; 32 float d1; 33 float e1;*/ 34 // Start is called before the first frame update 35 36 void Start() 37 { 38 StartCoroutine(Junbi()); 39 } 40 41 // Update is called once per frame 42 void Update() 43 { 44 45 } 46 /* 47 public void Jikkou1() 48 { 49 50 Roop(); 51 52 53 54 } 55 */ 56 public int Jikkou 57 { 58 // 取得 59 get { return z; } 60 61 // 変更 62 set { z = value; } 63 } 64 public IEnumerator Junbi() 65 { 66 yield return new WaitUntil(Z); 67 StartCoroutine(Jikkou1()); 68 StartCoroutine(Jikkou1_2()); 69 } 70 public IEnumerator Jikkou1_2() 71 { 72 List<float> notes1_2 = new List<float>(); 73 notes1_2.Clear(); 74 int y1 = notes1_2.Count; 75 y1 /= 5; 76 for (int i1 = 0; i1 <= y1 + 1; i1++) 77 { 78 if (x1 == 0) 79 { 80 notes1_2.Add(1); 81 notes1_2.Add(1); 82 notes1_2.Add(1); 83 notes1_2.Add(3.7f); 84 notes1_2.Add(6); 85 } 86 float a1 = notes1_2[x1]; 87 ++x1; 88 float b1 = notes1_2[x1]; 89 ++x1; 90 float c1 = notes1_2[x1]; 91 ++x1; 92 float d1 = notes1_2[x1]; 93 ++x1; 94 float e1 = notes1_2[x1]; 95 ++x1; 96 97 while (Music.IsJustChangedAt((int)a1, (int)b1, (int)c1) != true) 98 { 99 // 毎フレームループします 100 yield return null; 101 } 102 if (Mathf.Approximately(e1, 6.0f)) { 103 switch (d1) 104 { 105 case -7.4f: 106 Instantiate(Notes, new Vector3(d1, 6.0f, 0.0f), Quaternion.identity); 107 break; 108 case -3.7f: 109 Instantiate(Notes2, new Vector3(d1, 6.0f, 0.0f), Quaternion.identity); 110 break; 111 case 0: 112 Instantiate(Notes3, new Vector3(d1, 6.0f, 0.0f), Quaternion.identity); 113 break; 114 case 3.7f: 115 Instantiate(Notes4, new Vector3(d1, 6.0f, 0.0f), Quaternion.identity); 116 break; 117 case 7.4f: 118 Instantiate(Notes5, new Vector3(d1, 6.0f, 0.0f), Quaternion.identity); 119 break; 120 } 121 122 } 123 else 124 { 125 switch (d1) 126 { 127 case -7.4f: 128 Instantiate(Notes6, new Vector3(d1, -6.0f, 0.0f), Quaternion.identity); 129 break; 130 case -3.7f: 131 Instantiate(Notes7, new Vector3(d1, -6.0f, 0.0f), Quaternion.identity); 132 break; 133 case 0: 134 Instantiate(Notes8, new Vector3(d1, -6.0f, 0.0f), Quaternion.identity); 135 break; 136 case 3.7f: 137 Instantiate(Notes9, new Vector3(d1, -6.0f, 0.0f), Quaternion.identity); 138 break; 139 case 7.4f: 140 Instantiate(Notes10, new Vector3(d1, -6.0f, 0.0f), Quaternion.identity); 141 break; 142 } 143 } 144 145 146 } 147 } 148 public IEnumerator Jikkou1() 149 { 150 List<float> notes1 = new List<float>(); 151 notes1.Clear(); 152 int y = notes1.Count; 153 y /= 5; 154 for (int i = 0; i <= y + 1; i++) 155 { 156 if (x == 0) 157 { 158 notes1.Add(1); 159 notes1.Add(1); 160 notes1.Add(1); 161 notes1.Add(0); 162 notes1.Add(6); 163 notes1.Add(1); 164 notes1.Add(3); 165 notes1.Add(1); 166 notes1.Add(3.7f); 167 notes1.Add(-6); 168 } 169 float a = notes1[x]; 170 ++x; 171 float b = notes1[x]; 172 ++x; 173 float c = notes1[x]; 174 ++x; 175 float d = notes1[x]; 176 ++x; 177 float e = notes1[x]; 178 ++x; 179 180 while (Music.IsJustChangedAt((int)a, (int)b, (int)c) != true) 181 { 182 // 毎フレームループします 183 yield return null; 184 } 185 if (Mathf.Approximately(e, 6.0f)) 186 { 187 switch (d) 188 { 189 case -7.4f: 190 Instantiate(Notes, new Vector3(d, 6.0f, 0.0f), Quaternion.identity); 191 break; 192 case -3.7f: 193 Instantiate(Notes2, new Vector3(d, 6.0f, 0.0f), Quaternion.identity); 194 break; 195 case 0: 196 Instantiate(Notes3, new Vector3(d, 6.0f, 0.0f), Quaternion.identity); 197 break; 198 case 3.7f: 199 Instantiate(Notes4, new Vector3(d, 6.0f, 0.0f), Quaternion.identity); 200 break; 201 case 7.4f: 202 Instantiate(Notes5, new Vector3(d, 6.0f, 0.0f), Quaternion.identity); 203 break; 204 } 205 } 206 else if (Mathf.Approximately(e, -6.0f)) 207 { 208 switch (d) 209 { 210 case -7.4f: 211 Instantiate(Notes6, new Vector3(d, -6.0f, 0.0f), Quaternion.identity); 212 break; 213 case -3.7f: 214 Instantiate(Notes7, new Vector3(d, -6.0f, 0.0f), Quaternion.identity); 215 break; 216 case 0: 217 Instantiate(Notes8, new Vector3(d, -6.0f, 0.0f), Quaternion.identity); 218 break; 219 case 3.7f: 220 Instantiate(Notes9, new Vector3(d, -6.0f, 0.0f), Quaternion.identity); 221 break; 222 case 7.4f: 223 Instantiate(Notes10, new Vector3(d, -6.0f, 0.0f), Quaternion.identity); 224 break; 225 } 226 } 227 228 229 } 230 } 231 private bool Z() => z == 1; 232 void List2() 233 { 234 235 } 236 237} 238 239

試したこと

変数やリストを関数内で宣言しましたが、変わりませんでした

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

Unityのバージョン:2019.2.5f1

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

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

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

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

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

guest

回答1

0

ベストアンサー

ソースを眺めて確認しただけで動作はさせていませんが、コルーチンが動く順序次第で問題が起きているように見えます。
該当のエラー行を見ると、Jikkou1_2の配列要素数が0にも関わらず、2番目の要素を読もうとして例外が発生しています。
このHumen1スクリプトをアタッチしたゲームオブジェクトがシーンに複数存在していませんか?

a1~e1、a~eといった変数に値をセットしている部分は現状では常に同じ値になっているようですので、(コルーチンではなく)同期処理として実行する関数に切り出し、Start()でのコルーチンより前に値を設定するように変更するとシンプルに解決できそうです。

投稿2019/09/21 04:42

sayachang_bot

総合スコア228

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

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

roua

2019/09/21 05:38

本当にすみません、.1週間くらい前に始めた初心者なもので... 「同期処理として実行する関数に切り出し」というのは具体的にどういうことなのでしょうか? それと、Humen1スクリプトがアタッチされているオブジェクトはシーンに1つだけでした。
sayachang_bot

2019/09/23 20:21

関数の切り出しは以下のようなイメージでした。 private List<float> notes1_2; private void Setup() { notes1_2 = new List<float>(); notes1_2.Add(1); notes1_2.Add(1); notes1_2.Add(1); notes1_2.Add(3.7f); notes1_2.Add(6); } private void Start() { Setup(); StartCoroutine(Junbi()); } また、現状のコードではyやy1が常にゼロになっているので(notes1.Countしたタイミングではリストに要素が含まれていない)、それもあわせて解消できます。
roua

2019/09/23 22:35

本当ですね...yの定義場所明らかにおかしかったです>< 試してみます!!
roua

2019/09/23 22:36

行けました! 本当にありがとうございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問