Version: Unity 6.7 Beta (6000.7)
LanguageEnglish
  • C#

PhysicsBody.Destroy

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public bool Destroy(int ownerKey);

Parameters

Parameter Description
ownerKey Optional owner key returned when using PhysicsBody.SetOwner.

Returns

bool Returns true if the body was successfully destroyed. Otherwise, false.

Description

Destroy a body, destroying all attached PhysicsShape and PhysicsJoint. If the object is owned with PhysicsBody.SetOwner then you must provide the owner key it returned. Failing to do so will return a warning and the body will not be destroyed.

Unity produces an error in the Console window if you try to destroy a body that no longer exists. Check PhysicsBody.isValid first.

Additional resources: PhysicsWorld.DestroyBodyBatch

 // Create a physics body, then destroy it when it's no longer needed.
 using UnityEngine;
 using Unity.U2D.Physics;

public class DestroyBodyExample : MonoBehaviour { void Start() { PhysicsWorld world = PhysicsWorld.defaultWorld; PhysicsBody myBody = world.CreateBody(); myBody.Destroy(); } }