# RegisterDestructionCallback(Action<GameWindow>)

> Registers a callback that is invoked when the window is destroyed.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine.Windowing](/engine/6000.7/script-reference/unityengine/windowing.md)
* **Assembly:** UnityEngine.WindowingModule

```csharp
public void RegisterDestructionCallback(Action<GameWindow> callback)
```

### Parameters

**** (\[Action\<GameWindow>]\(https\://learn.microsoft.com/dotnet/api/system.action)): The callback to invoke when the window is destroyed, with the [GameWindow](/engine/6000.7/script-reference/unityengine/windowing/gamewindow.md) being destroyed as its parameter.

### Remarks

The callback runs while the window is still valid, which makes it the right place to clear any reference you hold to this [GameWindow](/engine/6000.7/script-reference/unityengine/windowing/gamewindow.md). After the callback returns, the instance is no longer valid and you must not use it.

This fires only for secondary windows. The main window can't be destroyed, so a callback registered on it never runs. Closing the main window quits the application instead.

### Examples

```csharp
using UnityEngine;
using UnityEngine.Windowing;

public class WindowingTest : MonoBehaviour
{
    GameWindow secondaryWindow;

    private void Start()
    {
        var settings = new GameWindowCreationSettings(
            title: "Secondary Window",
            width: 800,
            height: 600,
            position: new Vector2Int(0, 0),
            cameraDisplayIndex: 1,
            displayInfo: GameWindow.Main.GetDisplayInfo()
        );

        var op = GameWindowManager.Create(settings);
        op.completed += (AsyncOperation o) =>
        {
            secondaryWindow = ((CreateWindowAsyncOperation)o).window;

            // Register the callback after the window exists
            secondaryWindow.RegisterDestructionCallback(WindowDestroyedCallback);
        };
    }

    private void WindowDestroyedCallback(GameWindow window)
    {
        Debug.Log($"Window destroyed: {window.GetTitle()}");
        secondaryWindow = null;
    }
}
```
