前提・実現したいこと
オブジェクトをinstatiateをつかってシーン上に実体化させたい
ここに質問の内容を詳しく書いてください。
unityでinstatiate を使ってプレハブの実体化をしようと思ったのですがうまくできず、エラーメッセージとしてはオブジェクトが
指定されておらずnullであるといわれています。
■■な機能を実装中に以下のエラーメッセージが発生しました。
発生している問題・エラーメッセージ
NullReferenceException:
1cardGenerator.Start () (at Assets/sprict/cardGenerator.cs:26) 2 3エラーメッセージ
該当のソースコード
ソースコード ```c# using System.Collections; using System.Collections.Generic; using UnityEngine; public class cardGenerator : MonoBehaviour { public GameObject cardPrefab;//インスタンス化のための public GameObject hand;//親にする要素指定 List<cardData> cardDataList = new List<cardData>() { new cardData(10,"ヤドラン","水鉄砲"), new cardData(1,"ヤドラン","水鉄砲"), new cardData(10,"ヤドラン","水鉄砲"), new cardData(10,"ヤドラン","水鉄砲"), new cardData(10,"ヤドラン","水鉄砲"), }; void Start() { for ( int i = 0 ; i < cardDataList.Count; i++) { GameObject cardObj = Instantiate(cardPrefab);//生産 cardObj.transform.SetParent(hand.transform);//親指定 Card card = cardObj.GetComponent<Card>();//カードクラスのカード変数にスクリプト代入 card.Load(cardDataList[i]); } } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class cardData { public int hp; public string name; public string effect; public cardData(int _hp,string _name,string _effect) { hp = _hp; name = _name; effect = _effect; } } public class Card : MonoBehaviour { int hp; string Name; string effect; public void Load(cardData _cardData) { hp = _cardData.hp; Name = _cardData.name; effect = _cardData.effect; } } ### 試したこと ここに問題に対して試したことを記載してください。 ### 補足情報(FW/ツールのバージョンなど) ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/18 07:31
2019/04/18 07:39 編集
2019/04/18 11:52
2019/04/19 03:08