# CustomDestroy

### Introduction

The CustomDestroy (if using Glue) and Destroy (if not using Glue) methods are called when the Screen is going to be removed from your game. This method should never be manually called - the ScreenManager will call this after the Screen's [IsActivityFinished](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) property is set to true.

The purpose of the CustomDestroy/Destroy methods is to remove objects which have been manually added to your Screen. In general, any object which has been added to a FlatRedBall manager must be removed. Entities (which automatically add themselves) must be destroyed.

### Code Example

The following example shows how a [Sprite](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) and [Entity](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) can be created and destroyed properly. This uses the Glue custom methods, but the idea is the same.

The following are added at your Screen's class scope:

```
Sprite mMySprite;
MyEntityType mMyEntityInstance;
```

The following would be contained in CustomInitialize:

```
mMySprite = SpriteManager.AddSprite("redball.bmp", ContentManagerName);
mMyEntityInstance = new MyEntityType(ContentManagerName);
```

The following would be contained in CustomDestroy:

```
SpriteManager.RemoveSprite(mMySprite);
mMyEntityInstance.Destroy();
```

### For Glue Users

Keep in mind that only objects which are instantiated or added in your custom code need to be destroyed in the CustomDestroy method. In other words, if you make an instance of an [Entity](https://github.com/flatredball/FlatRedBallDocs/blob/main/frb/docs/index.php) in a Screen under the Objects item, you do not need to call Destroy on this object. Glue will both instantiate and destroy it automatically.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.flatredball.com/flatredball/glue-reference/screen/flatredball-screens-screen-customdestroy.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
