前提
Unityでハンドトラッキングを利用したシステムを作っています。
スクリプトをまたいでのListの利用方法が分からず困っています。
勉強中に身なのでつたない質問かと思いますがよろしくお願いします。
<作成したいシステム全体の概要>
UIのボタンをクリックした時に、人差し指の先の座標にオブジェクトを生成し、リストに座標値を格納する
→他のシーンで別のスクリプトから座標値を順次呼び出して再びオブジェクトを生成する
実現したいこと
- 作成されたListを他のスクリプトで利用したい
ヒエラルキー、インスペクターの画像
発生している問題
調べながら「試したこと」に記述したような変更をしてみたのですが、ここから他のスクリプトでどのように読み出せばよいのか分かりません。
もしくはそもそも現段階で間違っていたらご指摘頂けますと幸いです。
追記:hogefugapiyo様のコードを参考にさせて頂いたところ、CheckPos.csで[SerializeField] PositionList.r_index_finger_tip_marker;の最後のセミコロンに対してerror CS1519 Invalid token ';' in class, record, struct, or interface member declaration.が出ました。
該当のソースコード
C#
1//PositionList.cs 2public class PositionList : MonoBehaviour 3{ 4 public GameObject obj; 5 public int n; 6 public int i; 7 List<Vector3> posList = new List<Vector3>();//ご提示頂いたコードを参考にBottonClickSetからこちらに移動 8 public void ButtonClickSet() 9 { 10 //List<Vector3> posList = new List<Vector3>(); 11 for (i = 1; i < n; i++) 12 { 13 Transform myTransform = this.transform; 14 Vector3 worldPos = myTransform.position; 15 float x = worldPos.x; 16 float y = worldPos.y; 17 float z = worldPos.z; 18 19 Instantiate(obj, new Vector3(x, y, z), Quaternion.identity); 20 posList.Add(new Vector3(x, y, z)); 21 22 Debug.Log("Listの値" + posList[i]); 23 24 } 25 } 26 public List<Vector3> GetPosList() 27 { 28 return posList; 29 } 30}
試したこと
ご提示いただいたコードを該当コードに追加し、呼び出す側の別のスクリプト内に下記の様に記述しました。
C#
1//CheckPos.cs 2public class CheckPos : MonoBehaviour 3{ 4 [SerializeField] PositionList.r_index_finger_tip_marker;//error CS1519 発生場所 5 public int i; 6 7 pubilic void Check(){ 8 List<Vctor3> posListY = r_index_finger_tip_marker.GetPosList(); 9 10 for(int i = 0; i < posListY.Count;i++){ 11 Debug.Log(posListY[i]); 12 } 13 } 14}
補足情報(FW/ツールのバージョンなど)
Unity 2021.3.4f1
回答2件
あなたの回答
tips
プレビュー