■やりたい事
Instantiate
でランダムに生成され、移動しているprefab1
を
Button
を押すことでその瞬間のprefab1
の位置情報を取得し、得点として計算をした後に
Destroy
させるプログラムを書きたいのですが位置情報の取得ができず困っています…。
■実行したこと
Board
という空のゲームオブジェクトを作成し、
そこにBoardMove
とScoreCreate
のスクリプトをアタッチしprefab1
の生成と移動をさせました。
- BoardMove
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class BoardMove : MonoBehaviour { 6 7 // Use this for initialization 8 void Start () { 9 10 } 11 12 // Update is called once per frame 13 void Update () { 14 15 transform.localPosition = new Vector3 (-TimeManager.time * 100, 0, 0); 16 17 } 18}
- ScoreCreate
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ScoreCreator : MonoBehaviour { 6 7 public GameObject ScorePrefab; 8 public float timer; 9 10 private static float[] ScorePositionXList = new float[] { 11 -130, -20, 90, 200, 310 12 }; 13 14 // Use this for initialization 15 void Start () { 16 17 //タイマー初期化 18 timer = TimeManager.time + 1f; 19 20 //ランダム初期化 21 Random.seed = 100; 22 23 } 24 25 // Update is called once per frame 26 void Update () { 27 28 //xの位置決定 29 int rand = Random.Range(0,ScoreCreator.ScorePositionXList.Length); 30 float x = ScoreCreator.ScorePositionXList [rand]; 31 32 if (timer < TimeManager.time) { 33 34 //譜面作成 35 GameObject obj1 = Instantiate(ScorePrefab) as GameObject; 36 obj1.name = "prefab1"; 37 38 //ヒエラルキー 39 obj1.transform.parent = transform; 40 41 //位置 42 obj1.transform.localPosition = new Vector3(TimeManager.time * 100,x,0); 43 44 //表示順の移動 45 obj1.transform.SetAsFirstSibling(); 46 47 //サイズ// 48 obj1.transform.localScale = new Vector3(1,1,1); 49 50 //タイマー更新 51 timer = TimeManager.time + 1f; 52 53 } 54 55 } 56}
Prefab
をそのままDestroy(gameObject);
とすると生成されたPrefab(Clone)
ではなく
大元のPrefab
そのものがDestroy
されてしまうため、GameObject.Find("")
を使うことで
生成されたprefab1
を指定しました。
ただ、この状態だとDebug.Log(name);
で返ってくるのが大元のPrefab
の状態になってしまい
位置情報が固定されてしまっています。
- ScoreDelete
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ScoreDelete : MonoBehaviour { 6 7 // Use this for initialization 8 void Start () { 9 10 } 11 12 // Update is called once per frame 13 void Update () { 14 } 15 16 public void OnScoreClick(){ 17 18 Debug.Log (name); 19 Debug.Log (transform.localPosition); 20 21 Destroy (GameObject.Find("prefab1")); 22 23 } 24 25}
GetComponet
にて取得を試みてみたもののやり方がおかしいのか
そもそも間違えているのか上手くいかず…。
生成された個々のprefab
の位置情報の取得方法があれば教えて頂きたいです!
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/09 07:03
2018/05/09 08:02 編集