時間で自動的にGameObjectが消える(Destroy)スクリプト
エフェクトみたいな使い方で Prefab のインスタンス作ってあとは時間で自動的に消えてほしい時が結構あります。
そのための、 autoDestroy.cs というスクリプトです。
消したい GameObject にくっつけて life で 秒数を指定するだけで、
勝手にオブジェクトがデストロイです。
オブジェクトデストロイヤーです。
以下サンプルコード
using UnityEngine;
using System.Collections;
public class autoDestroy : MonoBehaviour {
public float life = 3.0f;
// Update is called once per frame
void Update () {
life -= Time.deltaTime;
if(life < 0)
{
Destroy(this.gameObject);
}
}
}