> For the complete documentation index, see [llms.txt](https://docs.flatredball.com/flatredball/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.flatredball.com/flatredball/api/flatredball/screens/screen/movetoscreen.md).

# MoveToScreen

### Introduction

The MoveToScreen method can be used to move from the current Screen to another Screen. The MoveToScreen method destroys the current Screen and all of its contained Entities, then begin loading the Screen passed to the MoveToScreen method.

MoveToScreen can be called when moving between different types of screens, such as between a TitleScreen and LevelSelectScreen. MoveToScreen should also be used to transition between levels such as moving between Level1 and Level2.

### Calling MoveToScreen

MoveToScreen accepts either the type (preferred if you know it) or the name of the screen. The screen name may be fully qualified (such as "YourGame.Screens.Level2") or not fully qualified (such as "Level2"). For example, to move to Level 2, the following code would be used:

```csharp
this.MoveToScreen(typeof(Level2));
```

Alternatively, you could pass in the name of Level2:

```csharp
this.MoveToScreen("YourProject.Screens.Level2");
```

You can go to the level if it is not fully qualified. This works too:

```csharp
this.MoveToScreen("Level2");
```

### Resetting a Screen

The `MoveToScreen` function does the following (in order):

1. Destroys the current Screen
2. Creates the next screen as specified by the argument to `MoveToScreen`.

`MoveToScreen` can be used to move to the same screen rather than a different screen. This results in the current screen being destroyed then recreated, resulting in the screen being reset to its original state. For example, consider a situation where the player's character is hit by a bullet. In this case the GameScreen will reset itself:

```csharp
// assuming there is a function to tell us if the player was hit by a bullet
// This also assumes that this code is written in the GameScreen and not in an entity
bool wasHitByBullet = GetIfHitByBullet();

if(wasHitByBullet)
{
   // GetType returns the GameScreen's type
   this.MoveToScreen(this.GetType());
}
```

### MoveToScreen Destroys the Screen

When the MoveToScreen method is called, the current Screen will be destroyed and the Screen that you are moving to will be created. The things that are destroyed are:

* Any files loaded through Glue for the current Screen or any Entities added to the Screen through Glue
* Any instances of Entities that have been added to Glue

If you have added objects that should be destroyed (such as additional Entities) in your custom code, then you need to make sure to destroy these objects in your CustomInitialize. For more information on whether you need to destroy an Entity or not, and how to destroy Entities which must be destroyed manually, see [the Destroying Entities article](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php).

### "The Screen that was just unloaded did not clean up after itself" Exception

For more information on this error and how to clean it up, see [Cleaning Up Screens](/flatredball/api/glue-runtime-api/glue-reference-screens-cleaning-up-screens.md).

### Passing information to new Screens

The MoveToScreen method has only one parameter - the Screen to move to. It does not accept additional parameters. For information on how to pass additional information to new Screens, see the the [Proper Information Access](https://github.com/flatredball/FlatRedBallDocs/tree/main/frb/docs/index.php) tutorial.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.flatredball.com/flatredball/api/flatredball/screens/screen/movetoscreen.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
