In this tutorial you will learn how to unload existing scenes and load a different one using a script.
Navigate to Source
directory, right-click, and select the option New -> Script. Then specify its name (eg. SceneChanger
) and hit Enter.
Here is a sample code that exposes a public variable with a reference to a scene asset that it should load. It checks in the Update function if the G
key was pressed, it then changes the current scene into the selected one.
[!code-csharpExample1]
[!code-cppExample2]
Now, add the script to an actor in your scene (select the actor and use Add script
button).
Then drag and drop the scene that you want to load from the Content Window into the asset picker.
Finally, hit the play button (or F5) and test the script logic by pressing the G
key. Your scene will be unloaded and a new scene will be loaded at runtime.