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