Unity Basics
  • Welcome
  • Getting Started with Unity Software
  • Helpful Shortcuts
  • The Unity Software Interface
    • The Default Interface
    • The Windows (Tabs)
      • Hierarchy Window
      • Scene Window
      • Project Window
      • Inspector Window
      • Game Window
      • Console Window
    • Other Layouts
      • Create a Custom Layout
  • Create
    • Creating Game Objects
      • 3D Primitives
      • Creating Game Objects in the Editor
      • Spawning Objects
      • Unhiding/Hiding Objects During Gameplay
      • Parenting
    • Creating Assets
    • Creating Components and Scripts
      • Adding Components in the Inspector
      • Creating Custom Components and Scripts
    • Creating Prefabs
  • Select and Update
    • General Selection
    • Search and Focus
    • Updating Game Objects
      • Updating Game Objects in the Editor
      • Updating Prefabs
    • Updating Assets
    • Updating Components
      • Update Components in the Inspector
      • Accessing Components Through Scripts
  • Delete
    • Deleting Game Objects
      • Deleting and Disabling Objects in the Editor
      • Using the Destroy() Function
    • Deleting Components and Scripts
      • Disabling and Removing Components in the Editor
      • Disabling and Removing Components During Gameplay
  • Translate, Rotate, and Scale
    • Intro to Transforms
    • Handy Transform Shortcuts
    • Translate
      • Positioning Game Objects in the Editor
      • Updating Position Through Script Code
      • Using the Transform.Translate() Function
    • Rotate
      • Rotating Game Objects in the Editor
      • Updating Rotation Through Script Code
      • Using the Transform.Rotate() Function
    • Scale
      • Resizing/Scaling Objects in the Editor
      • Updating Scale Through Script Code
    • Controlling Speed
    • Common Issues with Transforms
  • Materials
    • Material Basics
      • Creating and Applying Materials
      • Accessing Materials Through Code
  • Physics
    • Physics Basics
    • Colliders and Triggers
      • Collider Component Overview
      • Accessing Colliders Through Scripts
      • Common Issues: Colliders and Triggers
    • Rigidbody Component
      • Rigidbody Component Overview
      • Accessing the Rigidbody Component Through Scripts
      • Common Issues: Rigidbody Components
    • Common Issues: Physics and Velocity
  • Interaction
    • Interaction Basics
    • Keyboard Controls
    • Mouse Controls
  • Coding Basics
    • Intro to Scripts
    • Variables
    • Conditionals (If / Then / Else)
    • Arrays
    • Loops
    • Functions
Powered by GitBook
On this page
  • The Destroy() Function
  • Self-Destruction
  • Avoiding Errors

Was this helpful?

  1. Delete
  2. Deleting Game Objects

Using the Destroy() Function

Having too many items in a game could slow down the computer and its frame rate. This is especially apparent in games where objects are created during gameplay.

The Destroy() Function

To remove a game object or components of a game object, use the Destroy() function.

In the examples below, the following variables are used:

public GameObject thing;
public Transform thingTransform;

Example of deleting a game object:

Destroy(thing);

Example of deleting a game object of a transform:

Destroy(thingTransform.gameObject);

The Destroy() function can also delete components (colliders, Rigidbody, scripts, etc.).

Example of deleting a Rigidbody component on a game object:

Destroy(thing.GetComponent<Rigidbody>());

Self-Destruction

You can destroy the object the script is attached to. This is a great way of controlling prefabs that need to delete themselves.

Use this code:

Destroy(gameObject);

Avoiding Errors

You will get an error if you try to remove an object or component that is not there.

You can check to see if an object or component is there by using this script.

Example of checking if a variable has been assigned and if the associated game object exists:

if (thing != null)
{
     // There is an object assigned to the variable thing
}

Example of checking for a Rigidbody component:

if (thing.GetComponent<Rigidbody>() != null)
{
    // There is a Rigidbody component
}

PreviousDeleting and Disabling Objects in the EditorNextDeleting Components and Scripts

Last updated 4 years ago

Was this helpful?

Deleting parent objects will delete their child objects. Child objects can be moved to another parent if needed. Read more about .

Parenting
Accessing Components Through Scripts
Creating Custom Components and Scripts
Intro to Scripts