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

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

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

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

Unity

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

Q&A

解決済

1回答

13117閲覧

ArgumentOutOfRangeExceptionというエラーの対処とListの使い方があっているかわかりません

wata3

総合スコア16

C#

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

Unity

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

0グッド

0クリップ

投稿2020/06/15 10:23

前提・実現したいこと

Unityでカードゲームを作ろうとしています。
現在、デッキエディット画面を作ろうとしています。
前回の質問で長文になっていた検索機能を簡略化できそうなのですが、試しで一部のカードだけ表示しようとしたら下記のエラーが出ました。
Listにエクセルのデータをfor文で入れていこうとしています。
どのように対処すれば改善されますか?

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

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 <fb001e01371b4adca20013e0ac763896>:0) System.ThrowHelper.ThrowArgumentOutOfRangeException () (at <fb001e01371b4adca20013e0ac763896>:0) CardSearch.Start () (at Assets/DeckEdit/Script/CardSearch.cs:176)

176行目は下記のコードのStart()の閉じカッコ }に位置しています。
おそらくListの取り扱いが間違っているのだろうということしかわかりません。

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5using System; 6using System.Linq; 7public class CardModelU 8{ 9 public string id; 10 public string name1; 11 public string name2; 12 public int level; 13 public string type; 14 public int attack; 15 public int defence; 16 public string tag; 17 public string effect1; 18 public string effect2; 19 public string effect3; 20 // public string effect4; 21} 22public class CardModelS 23{ 24 public string id; 25 public string name1; 26 public string name2; 27 public int level; 28 public string type; 29 public string kind; 30 public string tag; 31 public string effect1; 32 public string effect2; 33 public string effect3; 34 public string effect4; 35} 36public class CardModelI 37{ 38 public string id; 39 public string name1; 40 public string name2; 41 public int level; 42 public string type; 43 public string kind; 44 public string tag; 45 public string effect1; 46 public string effect2; 47 public string effect3; 48 public string effect4; 49} 50public class CardSearch : MonoBehaviour 51{ 52 List<CardModelU> allUnitList = new List<CardModelU>(); 53 List<CardModelS> allSpelList = new List<CardModelS>(); 54 List<CardModelI> allItemList = new List<CardModelI>(); 55 public InputField Level; 56 public InputField ATK; 57 public InputField DEF; 58 public InputField Text; 59 public Dropdown kindDD1; 60 public Dropdown kindDD2; 61 public Dropdown Type; 62 //int Lv, atk, def,i; 63 public GameObject SearchArea; 64 public GameObject CardData; 65 public GameObject CardID_; 66 GameObject Obj; 67 [SerializeField] private unit unit; 68 [SerializeField] private item item; 69 [SerializeField] private spel spel; 70 71 public void CardSEARCH() 72 { 73 74 foreach (Transform child in SearchArea.transform) 75 { 76 Destroy(child.gameObject); 77 } 78 IEnumerable<CardModelU> result = allUnitList; 79 80 if (Level.text != "") 81 { 82 result = result.Where(s => s.level.ToString() == Level.text); 83 } 84 if (ATK.text != "") 85 { 86 result = result.Where(s => s.attack.ToString() == ATK.text); 87 } 88 if (DEF.text != "") 89 { 90 result = result.Where(s => s.defence.ToString() == DEF.text); 91 } 92 if (Text.text != "") 93 { 94 result = result.Where(s => s.name1.ToString().Contains(Text.text) || 95 s.name2.ToString().Contains(Text.text) || 96 s.effect1.ToString().Contains(Text.text) || 97 s.effect2.ToString().Contains(Text.text) || 98 s.effect3.ToString().Contains(Text.text)); 99 } 100 foreach (CardModelU modelU in result) ShowU(modelU); 101 } 102 public void ShowU(CardModelU modelU) 103 { 104 CardData.GetComponent<CardD>().ShowU(modelU); 105 Obj = Instantiate(CardData, this.transform.position, Quaternion.identity); 106 Obj.GetComponent<CardID>().cardID = modelU.id; //この辺のIDの取り回しは要確認 107 Obj.transform.parent = SearchArea.transform; 108 } 109 void Start() 110 { 111 int UD = unit.Sheet1.Count; 112 int SD = spel.Sheet1.Count; 113 int ID = item.Sheet1.Count; 114 for (int i = 0; i < UD; i++) 115 { 116 allUnitList[i].id = unit.Sheet1[i].id; 117 allUnitList[i].name1 = unit.Sheet1[i].name1; 118 allUnitList[i].name2 = unit.Sheet1[i].name2; 119 allUnitList[i].level = unit.Sheet1[i].level; 120 if (String.IsNullOrEmpty(unit.Sheet1[i].type) == false) 121 { 122 allUnitList[i].type = unit.Sheet1[i].type; 123 } 124 else 125 { 126 allUnitList[i].type = "なし"; 127 } 128 allUnitList[i].attack = unit.Sheet1[i].attack; 129 allUnitList[i].defence = unit.Sheet1[i].defence; 130 allUnitList[i].tag = unit.Sheet1[i].tag; 131 allUnitList[i].effect1 = unit.Sheet1[i].effect1; 132 if (String.IsNullOrEmpty(unit.Sheet1[i].effect2)==false) 133 { 134 allUnitList[i].effect2 = unit.Sheet1[i].effect2; 135 } 136 else 137 { 138 allUnitList[i].effect2 = "A"; 139 } 140 if (String.IsNullOrEmpty(unit.Sheet1[i].effect3) == false) 141 { 142 allUnitList[i].effect3 = unit.Sheet1[i].effect3; 143 } 144 else 145 { 146 allUnitList[i].effect3 = "B"; 147 } 148 // allUnitList[i].effect4 = unit.Sheet1[i].effect4; 149 } 150 /* for (int i = 0; i < SD; i++) 151 { 152 allSpelList[i].id = spel.Sheet1[i].id; 153 allSpelList[i].name1 = spel.Sheet1[i].name1; 154 allSpelList[i].name2 = spel.Sheet1[i].name2; 155 allSpelList[i].level = spel.Sheet1[i].level; 156 allSpelList[i].type = spel.Sheet1[i].type; 157 allSpelList[i].tag = spel.Sheet1[i].tag; 158 allSpelList[i].effect1 = spel.Sheet1[i].effect1; 159 allSpelList[i].effect2 = spel.Sheet1[i].effect2; 160 allSpelList[i].effect3 = spel.Sheet1[i].effect3; 161 allSpelList[i].effect4 = spel.Sheet1[i].effect4; 162 } 163 for (int i = 0; i < ID; i++) 164 { 165 allItemList[i].id = item.Sheet1[i].id; 166 allItemList[i].name1 = item.Sheet1[i].name1; 167 allItemList[i].name2 = item.Sheet1[i].name2; 168 allItemList[i].level = item.Sheet1[i].level; 169 allItemList[i].type = item.Sheet1[i].type; 170 allItemList[i].tag = item.Sheet1[i].tag; 171 allItemList[i].effect1 = item.Sheet1[i].effect1; 172 allItemList[i].effect2 = item.Sheet1[i].effect2; 173 allItemList[i].effect3 = item.Sheet1[i].effect3; 174 allItemList[i].effect4 = item.Sheet1[i].effect4; 175 }*/ 176 } 177}

試したこと

エクセルの空白入力がだめなのかと思い、空白セルのときに代入処理を任意の文字を入れようとしてみましたがだめでした。

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

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

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

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

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

sakura_hana

2020/06/15 11:07

質問の回答としてはtakapi_csさんので合っていますが気になった所を。 同じようなカードモデルクラスを3つ作る意味が分からないので1つに統一するか、せめて「クラスの継承」を調べて派生クラスにした方がいいと思います。(以前の質問の私の回答では統一させていた筈です)
wata3

2020/06/15 11:49 編集

エクセルのデータとの兼ね合いですね。 カード種ごとにエクセルのデータを作っており、IDはカード種ごとに通し番号になっています。 (現在Unitの場合CUU00001~CUU00182、Spellの場合CUS00001~CUS00166、Itemの場合CUI00001~CUI00066) あとはUnitとSpellとItemの持つ情報が少し異なるためです。 (Unitは攻撃力等がありSpellとItemにはなく、Spellには通常、永続、付与、Itemには通常、永続、装備など)
sakura_hana

2020/06/15 12:06

エクセルのデータが別々でも同じカードモデルクラスを用いることは可能です。どのタイプか+通し番号でIDが割り出せますし。 種類毎に情報が異なる場合、気にせず変数を全部乗せる(SpellとItemは攻撃力0扱いとする等)か、前述のクラス継承をした方がいいです。 でなければ今後もコードを書く量が3倍になるだけです(例えば現状のコードで検索出来るのはUnitだけで、SpellとItemを探す為には同じ量のコードを書く必要があります)。それで構わないというならいいですが。
wata3

2020/06/15 12:15

調べてみてできそうであれば挑戦してみます。ありがとうございます。
guest

回答1

0

ベストアンサー

Listにエクセルのデータをfor文で入れていこうとしています。

リストに対してデータを新規追加しようとしているのですね。
現在の実装では、allUnitListはnewのみ実行されており、Start()実行時のデータ個数は0です。
その状態でインデックスを使用した場合、allUnitList[0]は存在しないのでエラーとなります。
リストにデータを新規追加したい場合は、1つ適当な変数を用意してその中にデータを入れていき、データが整ってからリストに追加すると良いでしょう。
下記に例を記載しています。

C#

1var cardModelU = new CardModelU() 2{ 3 id = unit.Sheet1[i].id, 4 name1 = unit.Sheet1[i].name1, 5 ... 6}; 7allUnitList.Add(cardModelU);

投稿2020/06/15 10:58

takapi_cs

総合スコア349

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

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

wata3

2020/06/15 12:11

うまく行きそうです。ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問