List の中にある gameobject についている変数だけを取得したい
unityで、
パズルゲームを作っていて、触ったオブジェクトをListに入れて
listの中にあるオブジェクトの変数どうしをかけて組み合わせて
ある数になったときに イベントを起こすみたいなプログラムにしたいのですが
List の中にある gameobjectだけ についている変数だけを取得したいのです。
詳しい方お願いします。
こっちのやり方の方が早いよとか
もっと取り早い 方法があれば教えてください。
リストにオブジェクトを 追加して 消す処理のスクリプト
Unity
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using System; 5 6 7public class TouchManager : MonoBehaviour 8{ 9 // Start is called before the first frame update 10 void Start() 11 { 12 13 14 } 15 16 [SerializeField] 17 List<GameObject> touchBallList; 18 19 20 void Update() 21 { 22 var mousePos = Input.mousePosition; 23 // Debug.Log(mousePos); 24 if(Input.GetMouseButtonDown(0)) 25 { 26 touchBallList = new List<GameObject>(); 27 Ray ray = Camera.main.ScreenPointToRay(mousePos); 28 var h = Physics.RaycastAll(ray, 100.0f); 29 if(h.Length > 0) 30 { 31 //タッチしたボールが選択状態でないとき 32 if(h[0].collider.tag=="Ball"&&!h[0].collider.GetComponent<BallObject>().isTouch) 33 { 34 h[0].collider.GetComponent<BallObject>().isTouch = true; 35 touchBallList.Add(h[0].collider.gameObject); 36 } 37 } 38 } 39 if(Input.GetMouseButton(0)) 40 { 41 //選択しているボールが0個でないとき→タッチした場合 42 if(touchBallList.Count!=0) 43 { 44 Ray ray = Camera.main.ScreenPointToRay(mousePos); 45 var h = Physics.RaycastAll(ray, 100.0f); 46 if(h.Length > 0) 47 { 48 if( 49 h[0].collider.tag=="Ball"&&!h[0].collider.GetComponent<BallObject>().isTouch 50 ) 51 { 52 h[0].collider.GetComponent<BallObject>().isTouch = true; 53 touchBallList.Add(h[0].collider.gameObject); 54 } 55 } 56 } 57 } 58 if(Input.GetMouseButtonUp(0)) 59 { 60 ReleaseObject(); 61 } 62 63 } 64 65 public void ReleaseObject() 66 { 67 var cnt = touchBallList.Count; 68 //離したらマテリアルの色を戻す 69 foreach(GameObject go in touchBallList) 70 { 71 //選択状態を解除 72 go.GetComponent<BallObject>().isTouch = false; 73 //3個以上なら消す 74 if(cnt>=2) 75 { 76 Destroy(go); 77 } 78 } 79 touchBallList.Clear(); 80 } 81 }
取得したい変数
unity
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class O_ball : MonoBehaviour 6{ 7 public int number = 2; 8 9}
unity
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class H_ball : MonoBehaviour 6{ 7public int number = 13; 8} 9
Listとして扱いたい、このnumberという変数を持つクラスをわざわざ複数のクラスに別々で定義して扱いにくくする理由は何でしょうか?
回答2件
あなたの回答
tips
プレビュー