using System.Collections;using System.Collections.Generic;using UnityEngine;public class L14 : MonoBehaviour{ // Update is called once per frame void Update() { if(Input.GetKey(KeyCode.Space)) { Destroy(gameObject); //销毁对象 /*应用于不同情境下,有时不应该销毁对象。 否则脚本组件也会随之被删除。 */ } }}
using System.Collections;using System.Collections.Generic;using UnityEngine;public class L14 : MonoBehaviour{ public GameObject other; void Update() { if(Input.GetKey(KeyCode.Space)) { Destroy(other); //销毁其他对象 Destroy(GetComponent<MeshRenderer>()); //也可以移除某个组件而不是整个对象 Destroy(gameObject,3f); //延迟 3 秒后删除对象 } }}