Unity、プログラミングとも、初めてで、今年の9月くらいから少しずつ勉強してここまできました。
下記にヒントだけでもいただけますと大変助かります。
◇目的
Resourcesフォルダ内のPrefabを配列に代入して、タップした場所にランダムにそのアイテムを配置したいです。
◇問題点
下記のエラーの意味がわからず困っております。
InvalidCastException: Cannot cast from source type to destination type.
Click_gen_new.Start () (at Assets/Scripts/Click_gen_new.cs:20)
◇コード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Click_gen_new : MonoBehaviour 6{ 7 // 配列の指定 8 public GameObject[] Icon; 9 10 // クリックした位置座標 11 private Vector3 clickPosition; 12 13 // 配列から取り出し先 14 GameObject dropIcon; 15 16 17 // Use this for initialization 18 void Start() 19 { 20 Icon = (GameObject[])Resources.LoadAll("icons",typeof(GameObject)); 21 22 23 } 24 25 // Update is called once per frame 26 void Update() 27 { 28 // マウス入力で左クリックをした瞬間 29 if (Input.GetMouseButtonDown(0)) 30 { 31 dropIcon = Icon[Random.Range(0, Icon.Length)]; 32 33 // ここでの注意点は座標の引数にVector2を渡すのではなく、Vector3を渡すことである。 34 // Vector3でマウスがクリックした位置座標を取得する 35 clickPosition = Input.mousePosition; 36 37 // Z軸修正 38 clickPosition.z = 10f; 39 40 // オブジェクト生成 : オブジェクト(GameObject), 位置(Vector3), 角度(Quaternion) 41 // ScreenToWorldPoint(位置(Vector3)):スクリーン座標をワールド座標に変換する 42 GameObject inst = Instantiate(dropIcon, Camera.main.ScreenToWorldPoint(clickPosition), dropIcon.transform.rotation); 43 44 //レイヤーの付与。レイヤー付与までの時間後に実施 45 46 //レイヤー付与までの時間 47 //float TimeCount = 2.0f; 48 49 50 if (inst.tag != "not_toys") 51 { 52 inst.layer = LayerMask.NameToLayer("toys"); 53 } 54 55 56 57 } 58 59 } 60} 61
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/18 03:24
2018/12/18 04:15
2018/12/18 05:47