(スクリプトの作成)
- 新規にC#スクリプトの作成
- 名前を「AutoDestroy」に変更
- 下記のコードを書いてチェック
using UnityEngine;
public class AutoDestroy : MonoBehaviour
{
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
// Invoke関数・・・>時間差でメソッドを実行させたい場合に活用
// 数字は「秒」・・・>今回は、3秒後に実行する。
Invoke("DestroyMine", 3);
}
void DestroyMine()
{
Destroy(this.gameObject);
}
}
(実行確認)
- 好きなオブジェクトにスクリプトを追加
- ゲームを再生
- 3秒後に、自動的にオブジェクトが画面から消えれば成功です。