前提・実現したいこと
Unityでカードゲームを作ろうとしています。
前回の質問で無駄な長文が簡略化できそうなんですが、
試しで一部のカードだけ表示しようとしたら下記のエラーが出ました。
どこを修正したらいいと思いますか?
スクリプトを割り当てたオブジェクトの割り当て(?)はこうなっています。
発生している問題・エラーメッセージ
NullReferenceException: Object reference not set to an instance of an object CardSearch.Start () (at Assets/DeckEdit/Script/CardSearch.cs:115)
該当のソースコード
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 } 21public class CardModelS 22{ 23 public string id; 24 public string name1; 25 public string name2; 26 public int level; 27 public string type; 28 public string kind; 29 public string tag; 30 public string effect1; 31 public string effect2; 32 public string effect3; 33 public string effect4; 34} 35public class CardModelI 36{ 37 public string id; 38 public string name1; 39 public string name2; 40 public int level; 41 public string type; 42 public string kind; 43 public string tag; 44 public string effect1; 45 public string effect2; 46 public string effect3; 47 public string effect4; 48} 49public class CardSearch : MonoBehaviour 50{ 51 List<CardModelU> allUnitList; 52 List<CardModelS> allSpelList; 53 List<CardModelI> allItemList; 54 public InputField Level; 55 public InputField ATK; 56 public InputField DEF; 57 public InputField Text; 58 public Dropdown kindDD1; 59 public Dropdown kindDD2; 60 public Dropdown Type; 61 //int Lv, atk, def,i; 62 public GameObject SearchArea; 63 public GameObject CardData; 64 public GameObject CardID_; 65 GameObject Obj; 66 [SerializeField] private unit unit; 67 [SerializeField] private item item; 68 [SerializeField] private spel spel; 69 70 public void CardSEARCH() 71 { 72 foreach (Transform child in SearchArea.transform) 73 { 74 Destroy(child.gameObject); 75 } 76 IEnumerable<CardModelU> result = allUnitList; 77 78 if (Level.text != "") 79 { 80 result = result.Where(s => s.level.ToString() == Level.text); 81 } 82 if (ATK.text != "") 83 { 84 result = result.Where(s => s.attack.ToString() == ATK.text); 85 } 86 if (DEF.text != "") 87 { 88 result = result.Where(s => s.defence.ToString() == DEF.text); 89 } 90 if (Text.text != "") 91 { 92 result = result.Where(s => s.name1.ToString().Contains(Text.text) || 93 s.name2.ToString().Contains(Text.text) || 94 s.effect1.ToString().Contains(Text.text) || 95 s.effect2.ToString().Contains(Text.text) || 96 s.effect3.ToString().Contains(Text.text)); 97 } 98 foreach (CardModelU modelU in result) ShowU(modelU); 99 } 100 public void ShowU(CardModelU modelU) 101 { 102 CardData.GetComponent<CardD>().ShowU(modelU); 103 Obj = Instantiate(CardData, this.transform.position, Quaternion.identity); 104 Obj.GetComponent<CardID>().cardID = modelU.id; //この辺のIDの取り回しは要確認 105 Obj.transform.parent = SearchArea.transform; 106 } 107 void Start() 108 { 109 int UD = unit.Sheet1.Count; 110 int SD = spel.Sheet1.Count; 111 int ID = item.Sheet1.Count; 112 for (int i = 0; i < UD; i++) 113 { 114 allUnitList[i].id = unit.Sheet1[i].id; 115 allUnitList[i].name1 = unit.Sheet1[i].name1; 116 allUnitList[i].name2 = unit.Sheet1[i].name2; 117 allUnitList[i].level = unit.Sheet1[i].level; 118 allUnitList[i].type = unit.Sheet1[i].type; 119 allUnitList[i].attack = unit.Sheet1[i].attack; 120 allUnitList[i].defence = unit.Sheet1[i].defence; 121 allUnitList[i].tag = unit.Sheet1[i].tag; 122 allUnitList[i].effect1 = unit.Sheet1[i].effect1; 123 allUnitList[i].effect2 = unit.Sheet1[i].effect2; 124 allUnitList[i].effect3 = unit.Sheet1[i].effect3; 125 // allUnitList[i].effect4 = unit.Sheet1[i].effect4; 126 } 127 for (int i = 0; i < SD; i++) 128 { 129 allSpelList[i].id = spel.Sheet1[i].id; 130 allSpelList[i].name1 = spel.Sheet1[i].name1; 131 allSpelList[i].name2 = spel.Sheet1[i].name2; 132 allSpelList[i].level = spel.Sheet1[i].level; 133 allSpelList[i].type = spel.Sheet1[i].type; 134 allSpelList[i].tag = spel.Sheet1[i].tag; 135 allSpelList[i].effect1 = spel.Sheet1[i].effect1; 136 allSpelList[i].effect2 = spel.Sheet1[i].effect2; 137 allSpelList[i].effect3 = spel.Sheet1[i].effect3; 138 allSpelList[i].effect4 = spel.Sheet1[i].effect4; 139 } 140 for (int i = 0; i < ID; i++) 141 { 142 allItemList[i].id = item.Sheet1[i].id; 143 allItemList[i].name1 = item.Sheet1[i].name1; 144 allItemList[i].name2 = item.Sheet1[i].name2; 145 allItemList[i].level = item.Sheet1[i].level; 146 allItemList[i].type = item.Sheet1[i].type; 147 allItemList[i].tag = item.Sheet1[i].tag; 148 allItemList[i].effect1 = item.Sheet1[i].effect1; 149 allItemList[i].effect2 = item.Sheet1[i].effect2; 150 allItemList[i].effect3 = item.Sheet1[i].effect3; 151 allItemList[i].effect4 = item.Sheet1[i].effect4; 152 } 153 } 154}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/06/14 22:30