Unity ブログ -Game Clearの定義-
『childCount』の使い方
私はこのように使っております。
if(this.transform.childCount == 0)
{
Destroy(gameObject);
}
こう書くことで「このオブジェクトの子オブジェクトの数が0になったら、このゲームオブジェクトを壊す」となります。
空のオブジェクトの中にブロックを入れておき、全部ブロックがなくなったら空のオブジェクトが壊れたり、0になったときにまた何かをするなどのフラグとしても有効です。
今回、ブロック内のスクリプトを
void OnCollisionEnter(Collision collision)
{
Destroy(gameObject);
}
using UnityEngine.UI;
using TMPro;
Transform myTransform;
public GameObject targetbutton;
public GameObject TextMPgameover3;
public GameObject targetball2;
public GameObject player2;
void Start()
{
myTransform = transform;
}
void Update()
{
if (myTransform.childCount == 0)
{
TextMPgameover3.SetActive(true); //game clear
targetbutton.SetActive(true); //reward
targetball2.GetComponent<Rigidbody>().velocity = Vector3.zero;
targetball2.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
player2.GetComponent<Rigidbody>().velocity = Vector3.zero;
player2.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
}
}
『using TMPro;』