Search Results for

    Show / Hide Table of Contents

    HOWTO: Change scene from script

    In this tutorial you will learn how to unload existing scenes and load a different one using a script.

    1. Prepare a new script

    Navigate to Source directory, right-click, and select the option New -> Script. Then specify its name (eg. SceneChanger) and hit Enter.

    2. Implement scene change logic

    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.

    • C#
    • C++
    using FlaxEngine;
    
    public class SceneChanger : Script
    {
        public SceneReference AnotherScene;
    
        public override void OnUpdate()
        {
            if (Input.GetKeyDown(KeyboardKeys.G))
                Level.ChangeSceneAsync(AnotherScene);
        }
    }
    
    #pragma once
    
    #include "Engine/Scripting/Script.h"
    #include "Engine/Content/SceneReference.h"
    #include "Engine/Input/Input.h"
    #include "Engine/Level/Level.h"
    
    API_CLASS() class GAME_API SceneChanger : public Script
    {
        API_AUTO_SERIALIZATION();
        DECLARE_SCRIPTING_TYPE(SceneChanger);
    
        API_FIELD()
        SceneReference AnotherScene;
    
        // [Script]
        void OnUpdate() override
        {
            if (Input::GetKeyDown(KeyboardKeys::G) && AnotherScene.ID.IsValid())
            {
                // Does the same as the C# API
                Level::UnloadAllScenesAsync();
                Level::LoadSceneAsync(AnotherScene.ID);
            }
        }
    };
    

    3. Add script

    Now, add the script to an actor in your scene (select the actor and use Add script button).

    Change Scene From Code

    4. Assign scene

    Then drag and drop the scene that you want to load from the Content Window into the asset picker.

    Change Scene From Code

    5. Test it out

    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.

    • Improve this Doc
    In This Article
    Back to top Copyright © 2012-2023 Wojciech Figat