りんごと爆弾が落ちてくるゲームで落ちてきたものと同じボタン(BombについてもAppleButtonとおなじようなスクリプトを実装予定)を押すとアイテムをゲットできそのアイテムが画面から消えるという機能を実装したいです。下記のようにコードを書いてみたのですがButtonをタップしても画面上にアイテムが残ったままになってしまいます。
どのようにすればアイテムを消すことができますか?
ゆくゆくはアニメーションを付けて消えていくといった風にしたいのですがその場合も同じやり方でできるのでしょうか?
よろしくお願いします。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ItemGenerator: MonoBehaviour { float span = 1.0f; float delta = 0; public float dropSpeed = -0.03f; public GameObject Apple; public GameObject Bomb; GameObject director; public List<GameObject> items= new List<GameObject>(); // Use this for initialization void Start () { } // Update is called once per frame void Update () { this.delta += Time.deltaTime; if (this.delta > this.span) { this.delta = 0; int random = Random.Range(1, 3); //りんごを落とす if(random == 1) { items.Add(Instantiate(Apple) as GameObject); } //爆弾を落とす else { items.Add(Instantiate(Bomb) as GameObject); } } } public void Destroy() { Destroy(items[0]); } } コード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AppleButton : MonoBehaviour { GameObject director; GameObject generator; // Use this for initialization void Start () { this.director = GameObject.Find("GameDirector"); this.generator = GameObject.Find("ItemGenerator"); } // Update is called once per frame void Update () { } public void OnClick() { if(generator.GetComponent<ItemGenerator>().items[0] == generator.GetComponent<ItemGenerator>().Apple) { generator.GetComponent<ItemGenerator>().Destroy(); generator.GetComponent<ItemGenerator>().items.RemoveAt(0); director.GetComponent<GameDirector>().Get(); } else { director.GetComponent<GameDirector>().GameOver(); } } } コード
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/25 04:28 編集
2018/04/25 05:06
2018/04/25 05:26
2018/04/25 06:08 編集
2018/04/26 02:12
2018/04/26 02:18 編集
2018/04/26 02:20