(Unity6_BRP)ボタンを押してオブジェクトを破壊する

(スクリプトの作成)

  • 新規にC#スクリプトの作成
  • 名前を「DestroyObject」に変更
  • 下記のコードを書いてチェック
using UnityEngine;

public class DestroyObject : MonoBehaviour
{
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.U))
        {
            Destroy(this.gameObject);
        }
    }
}

(実行確認)

  • 好きなオブジェクトにスクリプトを追加
  • ゲームを再生
  • 「U」ボタンを押した瞬間にオブジェクトが消えれば成功です。