1 回答

TA貢獻(xiàn)2019條經(jīng)驗(yàn) 獲得超9個(gè)贊
如果您傳入對?的?Component
?引用,它只會(huì)銷毀相應(yīng)的組件,但保留其余的?未受影響Destroy
GameObject
對象 obj 將立即被銷毀,或者如果指定時(shí)間 t 秒后將被銷毀。?如果 obj 是一個(gè)
Component
,它將從?GameObject
?中刪除該組件并銷毀它?。如果 obj 是?GameObject
,它將銷毀?GameObject
、其所有組件以及?GameObject
?的所有變換子級。
Destroy(damageDealer.GetComponnet<Rigidbody2D>());
如果您經(jīng)常這樣做,最好將此引用存儲(chǔ)在?組件的?Awake
?中,然后像DamageDealer
?一樣傳遞它
public class DamageDealer : MonoBehaviour
{
? ? // if possible already reference this via the Inspector
? ? [SerializeField] private Rigidbody2D rigidbody;
? ? // This is a read-only property returning the value of rigidbody
? ? public Rigidbody2D Rigidbody => rigidbody;
? ? private void Awake()
? ? {
? ? ? ? if(!rigidbody) rigidbody = GetComponnet<Rigidbody2D>();
? ? ? ? ...
? ? }
? ? ...
}
然后你可以簡單地做
Destroy(damageDealer.Rigidbody);
- 1 回答
- 0 關(guān)注
- 171 瀏覽
添加回答
舉報(bào)