前提・実現したいこと
前提
インスタンスの扱い方、プログラムの書き方への理解が浅いです。
ItemDataというクラスとItemDataBaseというクラスがあります(ファイルは別)。
ItemDataBase内でItemDataクラスからインスタンス?を作ろうとするとエラーが出てしまいます。
発生している問題・エラーメッセージ
You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all UnityEngine.MonoBehaviour:.ctor() ItemData:.ctor(Sprite, String, Item, String)
該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ItemData : MonoBehaviour { Sprite itemimage; string itemname,iteminfo; // アイテムのタイプ private MyItem.Item itemType; public ItemData(Sprite image, string itemName,MyItem.Item itemType, string information) { this.itemimage = image; this.itemname = itemName; this.itemType = itemType; this.iteminfo = information; } public Sprite GetItemSprite() { return itemimage; } public string GetItemName() { return itemname; } public MyItem.Item GetItemType() { return itemType; } public string GetItemInformation() { return iteminfo; } } using System.Collections; using System.Collections.Generic; using UnityEngine; public class ItemDataBase : MonoBehaviour { ItemData[] itemlist = new ItemData[2]; // Start is called before the first frame update void Awake() { itemlist [0] = new ItemData (Resources.Load ("aaa", typeof(Sprite))as Sprite, "namae",MyItem.Item.FlashLight, "info"); itemlist [1] = new ItemData (Resources.Load ("bbb", typeof(Sprite))as Sprite, "namae",MyItem.Item.FlashLight, "info"); } public ItemData[] GetItemData() { return itemlist; } public int GetItemTotal() { return itemlist.Length; } }
試したこと
ItemDataBaseのnewを消してみたが、ほかのエラーが出てうまく行かない
ネットを検索してみたが、解決できなかった
補足情報(FW/ツールのバージョンなど)
Unity 2018.3.3f1
よろしくお願いいたします
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/17 14:26