Zhonghui

每个不曾起舞的日子,都是对生命的辜负

User Tools

Site Tools


软件:unity:销毁游戏对象

销毁游戏对象

使用Destroy函数


using UnityEngine;
 
public class ScriptExample : MonoBehaviour
{
    void DestroyGameObject()
    {
        Destroy(gameObject);
    }
 
    void DestroyScriptInstance()
    {
        // Removes this script instance from the game object
        Destroy(this);
    }
 
    void DestroyComponent()
    {
        // Removes the rigidbody from the game object
        Destroy(GetComponent<Rigidbody>());
    }
 
    void DestroyObjectDelayed()
    {
        // Kills the game object in 5 seconds after loading the object
        Destroy(gameObject, 5);
    }
 
    // When the user presses Ctrl, it will remove the
    // BoxCollider component from the game object
    void Update()
    {
        if (Input.GetButton("Fire1") &amp;&amp; GetComponent<BoxCollider>())
        {
            Destroy(GetComponent<BoxCollider>());
        }
    }
}
/var/www/DokuWikiStick/dokuwiki/data/pages/软件/unity/销毁游戏对象.txt · Last modified: 2022/09/06 01:38 by zh